创建 knex 迁移
Creating knex migration
我发现有两种方法可以在迁移文件中编写 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 at 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 脚本的正确方法,因为承诺是在 knex 中处理查询的首选方式,因为它们允许您 return 来自履行处理程序的值。
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();
})
]);
}
我发现有两种方法可以在迁移文件中编写 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 at 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 脚本的正确方法,因为承诺是在 knex 中处理查询的首选方式,因为它们允许您 return 来自履行处理程序的值。
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();
})
]);
}