【发布时间】:2020-03-17 21:49:58
【问题描述】:
我有一个使用 sequelize create 方法将事件添加到数据库的发布路线。我已经为用户和事件定义了表:
活动
const Events = sequelize.define('Events', {
Event_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
month: {
type: DataTypes.STRING,
allowNull: false
},
day:{
type:DataTypes.INTEGER,
allowNull: false
},
year: {
type: DataTypes.INTEGER,
allowNull: false
},
important:{
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
description:{
type: DataTypes.STRING,
allowNull: true
}
});
return Events;
}
用户
const User = sequelize.define('Users', {
user_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false
},
password: {
type: DataTypes.STRING,
allowNull: false
}
}, {});
User.associate = function(models){
User.hasMany(models.Events, {as: 'user'})
}
return User;
}
post 路由似乎运行正常,除了 sequelize 无法将用户 ID 附加到创建的事件做到
"无法添加或更新子行:外键约束失败 (
planitdb.events, CONSTRAINTevents_ibfk_1FOREIGN KEY (UserUserId) REFERENCESusers(user_id) ON DELETE SET NULL更新级联)"
我不确定我是否完全理解为什么会发生这种情况。我想我还需要在事件表中定义关系吗?如果是这种情况,我不确定我是否了解 Events 表与 User 表之间的关系类型。或者这种关系是对单个用户的单个事件?
任何帮助将不胜感激。
【问题讨论】:
标签: mysql node.js sequelize.js