Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Migration 迁移:在特定列之后添加列_Migration_Knex.js - Fatal编程技术网

Migration 迁移:在特定列之后添加列

Migration 迁移:在特定列之后添加列,migration,knex.js,Migration,Knex.js,使用SQL很容易做到这一点,但我需要编写一个我不熟悉的Knex迁移脚本。以下内容将在order表的行末尾添加order\u id列。我想在id之后添加order\u id。我该怎么做 const TABLE_NAME = 'order'; exports.up = function (knex) { return knex.schema.alterTable(TABLE_NAME, table => { table .specificType

使用SQL很容易做到这一点,但我需要编写一个我不熟悉的Knex迁移脚本。以下内容将在
order
表的行末尾添加
order\u id
列。我想在
id
之后添加
order\u id
。我该怎么做

const TABLE_NAME = 'order';
exports.up = function (knex) {
    return knex.schema.alterTable(TABLE_NAME, table => {
        table
            .specificType('order_id', 'char(10)')
            .unique()
            .notNullable();

    });
};

exports.down = function (knex) {
    return knex.schema.table(TABLE_NAME, function (t) {
        t.dropColumn('order_id');
    });
};

没有SQL确定更改表时的列顺序。在查询时确定顺序,例如:

knex
  .select('id', 'order_id')
  .from('order')
屈服

id | order_id
===+=========
1  | 2
order_id | id
=========+===
2        | 1
鉴于

knex
  .select('order_id', 'id')
  .from('order')
屈服

id | order_id
===+=========
1  | 2
order_id | id
=========+===
2        | 1

如果单个数据库引擎确实支持在创建表之后更改列的顺序,那么这将是该引擎特有的,对于Knex这样的SQL生成器来说并不容易操作。有关更多信息,请参阅。

旧主题,但我找到了一个答案:

return knex.schema.table('the_table', table => {    
    table.tinyint('new_column').defaultTo('0').after('other_column')
})
这是Mysql数据库上的工作