【发布时间】:2017-12-12 10:08:17
【问题描述】:
我对几乎所有事情都很陌生。我尝试使用单个迁移文件在 MySQL 数据库中创建所有表(20 个表)。
exports.up = function(knex, Promise) {
function createTable1() {
return knex.schema.createTableIfNotExists('Table1', (t) => {
t.increments('id').primary();
t.string('col_1', 48).unique().notNullable();
t.timestamps(true, true);
}).catch((e) => console.log(e));
}
function createTable2() {
return knex.schema.createTableIfNotExists('Table2', (t) => {
t.increments('id').primary();
t.string('col_1', 48).unique().notNullable();
t.integer('someId').unsigned().references('Table1.id')
t.timestamps(true, true);
}).catch((e) => console.log(e));
}
function createTable3() {
return knex.schema.createTableIfNotExists('Table3', (t) => {
t.increments('id').primary();
t.string('col_1', 48).unique().notNullable();
t.integer('someId').unsigned().references('Table1.id')
t.integer('someOtherId').unsigned().references('Table2.id')
t.timestamps(true, true);
}).catch((e) => console.log(e));
}
... //similar functions for all 20 tables
return Promise.all([
createTable1()
.then(createTable2())
.then(createTable3())
...
.then(createTable20())
.catch((e) => console.log(e.sql))
]);
}
exports.down = function(knex, Promise) {
return knex.schema.dropTable('Table1')
.then(knex.schema.dropTable('Table2'))
.then(knex.schema.dropTable('Table3'))
...
.then(knex.schema.dropTable('Table20'))
.catch((e) => console.log(e.sql))
};
我希望 knex 在一个事务中执行所有 sql 查询
迁移执行但产生以下错误:
未处理的拒绝错误:事务查询已完成,运行 使用 DEBUG=knex:tx 了解更多信息
诚然,我没有牢牢掌握如何正确使用 Promise,并且我知道 return Promise.all 块不一定会以相同的顺序生成和执行 SQL 查询,但我应该这样做吗?为每个表创建单独的迁移文件是否更有意义?
【问题讨论】:
-
> 为每个表创建单独的迁移文件是否更有意义?我认为这样做更常见
标签: javascript mysql node.js innodb knex.js