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

use*_*690 3 migration knex.js

使用 SQL 很容易做到这一点,但我需要编写一个我不熟悉的 Knex 迁移脚本。以下将在表order_id中的行末尾添加列order。我想order_id在之后添加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');
    });
};
Run Code Online (Sandbox Code Playgroud)

Lex*_*tas 5

老话题,但我找到了答案:

return knex.schema.table('the_table', table => {    
    table.tinyint('new_column').defaultTo('0').after('other_column')
})
Run Code Online (Sandbox Code Playgroud)

这适用于 Mysql 数据库。