Migration 迁移:在特定列之后添加列
使用SQL很容易做到这一点,但我需要编写一个我不熟悉的Knex迁移脚本。以下内容将在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
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数据库上的工作