无法使用semelize添加外键约束
尝试使用quelize创建一个待办事项列表,但我总是遇到这个错误: “也无法使用semelize添加外键约束”,即使在Stackoverflow中寻找一些线索,Somone也可以提供帮助?
!
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Todos', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
description: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
status_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Statuses',
key: 'id',
},
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('Todos');
},
};
如果有人有任何想法,
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Statuses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('Statuses');
},
};
Relationship in models
// Status model
Status.associate = (models) => {
Status.belongsTo(models.Todo, { foreignKey: 'status_id', as: 'todo' });
};
// Todo model
Todo.associate = (models) => {
Todo.hasMany(models.Status, { foreignKey: 'status_id', as: 'statuses' });
};
我真的很感激 谢谢
Trying to create a todo list with sequelize but I always hava this error:
"Cannot add foreign key constraint using Sequelize" Even looking for some clues in StackOverflow, somone can help?
ToDo Migration
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Todos', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
description: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
status_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Statuses',
key: 'id',
},
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('Todos');
},
};
Status migration
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('Statuses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
});
},
async down(queryInterface) {
await queryInterface.dropTable('Statuses');
},
};
Relationship in models
// Status model
Status.associate = (models) => {
Status.belongsTo(models.Todo, { foreignKey: 'status_id', as: 'todo' });
};
// Todo model
Todo.associate = (models) => {
Todo.hasMany(models.Status, { foreignKey: 'status_id', as: 'statuses' });
};
If someone have any idea I'm really appreciate! Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当“ todos”表具有“ status_id”时,
它必须“属于“状态”表。
或者,您应该将“ todo_id”保存在“状态”表中。
When the "Todos" table has the "status_id",
it must "belong to" the "Statuses" table.
Or you should save the "todo_id" in the "Statuses" table.