我尝试使用Knex.js查询构建器构建数据库迁移,但是我收到与外键关系有关的以下错误:
Knex:warning - migrations failed with error: alter table `presentations` add constraint presentations_owner_id_foreign foreign key (`owner_id`) references `users` (`user_id`) - ER_CANT_CREATE_TABLE: Can't create table `application`.`#sql-35c_6a` (errno: 150 "Foreign key constraint is incorrectly formed")
这是presentations
表的迁移:
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('presentations', function(table) {
table.increments('presentation_id');
table.string('title', 255);
table.integer('owner_id');
table.boolean('is_live');
table.timestamps();
table.foreign('owner_id').references('user_id').inTable('users');
})
]);
};
这是users
表的迁移:
exports.up = function(knex, Promise) {
return Promise.all([
knex.schema.createTable('users', function(table) {
table.increments('user_id');
table.string('first_name');
table.string('last_name');
table.string('email_address').unique();
table.string('password');
...
table.timestamps();
})
]);
};
不同的数据类型是否有问题?我正在尝试建立integer
和increments
类型之间的关系。