gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:50:46 26 4
gpt4 key购买 nike

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

最佳答案

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

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

这适用于 Mysql 数据库。

关于迁移:在特定列之后添加一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58828429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com