gpt4 book ai didi

knex.js - 创建 knex 迁移

转载 作者:行者123 更新时间:2023-12-04 00:10:31 27 4
gpt4 key购买 nike

我发现有两种方法可以在迁移文件中编写 knex 迁移。

exports.up = function (knex) {
return knex.schema
.createTableIfNotExists('foo', function (table) {
table.increments('id').unique();
table.string('foo1');
table.string('foo2');
})
.createTableIfNotExists('bar', function (table) {
table.increments('bar1');
table.string('bar2').index();
});

或者

exports.up = function (knex) {
return Promise.all([
knex.schema.createTableIfNotExists('foo', function (table) {
table.increments('id').unique();
table.string('foo1');
table.string('foo2');
}),
knex.schema.createTableIfNotExists('bar', function (table) {
table.increments('bar1');
table.string('bar2').index();
})
]);
}

哪一个是正确的做法?

最佳答案

Ricardo Graca 回答在 Knex's github issue page

In that case it doesn't make a difference.

You would only use the Promise based one if you require some change in a table before doing another change in another table. For example, if you needed to reference a certain table from another table and none of those tables exist yet, you would create the first table (the one that doesn't depend on anything) in a promise and then when that promise resolved you would create the second table. That way you ensure that dependencies are met.

关于knex.js - 创建 knex 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36431899/

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