我想在我的ts项目中使用knex。 迁移如下所示:
export async function up(knex: Knex): Promise<any> {
return knex.schema.createTable('first_table', (t) => {
t.integer('first_table_id').primary();
}).createTable('second_table', (t) => {
t.integer('id');
// simulation sql error, duplicate in this case.
t.integer('id').
})
}
迁移已失败。我等待事务回滚所有更改,但我已成功创建first_table。我不明白什么或 Knex 行为不正确?
当mysql执行DDL查询时,它会隐式提交。因此,在创建表事务的每个查询之后,将自动提交。
除了在运行迁移之前进行数据库转储并在迁移失败时还原它之外,没有真正简单的解决方法。
https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html
例如,如果您执行以下操作,则使用 mysql:
* start transaction
* create table 1
* rollback
表 1 仍将创建并且没有回滚,因为内部 mysql 实际上确实:
* start transaction
* create table 1
* implicit commit
* implicit start transaction for rollback query
* rollback of automatically started empty transaction