我有一个问题,我无法使用特定的对象值来创建记录。当它执行INSERT命令时,在命令开始后立即抛出错误,并且永远不会创建记录。在我的路线中,错误出现在userId:user.user_id
。
Unhandled rejection SequelizeForeignKeyConstraintError: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`test_db`.`member`, CONSTRAINT `member_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `organization` (`organization_id`) ON DELETE CASCADE ON UPDATE CASCADE)
以下是模型上的关系:
以下是记录的SQL:
Executing (default): INSERT INTO `user` (`user_id`,`email`,`organization_id`,`authentication_token`,`updatedAt`,`createdAt`) VALUES (DEFAULT,'tester@vdfd.com','1','f800127f4b345d1af54d23f566eca88859898d70','2016-05-30 21:39:19','2016-05-30 21:39:19');
This user:[object SequelizeInstance:user]
41
Executing (default): INSERT INTO `member` (`member_id`,`member_email`,`organization_id`,`user_id`,`updatedAt`,`createdAt`) VALUES (DEFAULT,'tester@vdfd.com','1',41,'2016-05-30 21:39:19','2016-05-30 21:39:19');
路由:
.post(function(req, res, user){
var token;
User.create({
email: req.body.email,
organizationId: req.body.organizationId,
authenticationToken: crypto.randomBytes(20).toString('hex')
}).then(function(user){
token = user.authenticationToken;
console.log('This user:' + user);
console.log(user.user_id);
return Member.create({
organizationId: req.body.organizationId,
memberEmail: req.body.email,
userId: user.user_id
});
}).then(function(authToken){
console.log(authToken);
console.log("Success");
res.redirect('/app/settings/add-users');
})
});
ember.js:
module.exports = function(sequelize, DataTypes) {
var Member = sequelize.define('member', {
memberId: {
type: DataTypes.INTEGER,
field: 'member_id',
autoIncrement: true,
primaryKey: true
},
memberEmail: {
type: DataTypes.STRING,
field: 'member_email',
isEmail: true,
unique: true
},
organizationId: {
type: DataTypes.INTEGER,
field: 'organization_id',
allowNull: true
},
userId: {
type: DataTypes.INTEGER,
field: 'user_id',
allowNull: true
}
},{
freezeTableName: true,
});
return Member;
}
user.js:
var bcrypt = require('bcrypt-nodejs');
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('user', {
user_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
},
email: {
type: DataTypes.STRING,
isEmail: true,
unique: true,
set: function(val) {
this.setDataValue('email', val.toLowerCase());
}
},
password: DataTypes.STRING,
organizationId: {
type: DataTypes.INTEGER,
field: 'organization_id',
allowNull: true
},
authenticationToken: {
type: DataTypes.STRING,
field: 'authentication_token'
},
resetPasswordToken: {
type: DataTypes.STRING,
field: 'reset_password_token'
},
resetPasswordExpires: {
type: DataTypes.DATE,
field: 'reset_password_expires'
}
}, {
freezeTableName: true,
classMethods: {
associate: function(db) {
User.belongsToMany(db.Organization, { through: 'member', foreignKey: 'organizationId'})
},
generateHash: function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
},
},
instanceMethods: {
validPassword: function(password) {
return bcrypt.compareSync(password, this.password);
},
},
});
return User;
}
Organation.js:
module.exports = function(sequelize, DataTypes) {
var Organization = sequelize.define('organization', {
organizationId: {
type: DataTypes.INTEGER,
field: 'organization_id',
autoIncrement: true,
primaryKey: true
},
organizationName: {
type: DataTypes.STRING,
field: 'organization_name'
},
admin: DataTypes.STRING
},{
freezeTableName: true,
classMethods: {
associate: function(db) {
Organization.belongsToMany(db.User, { through: 'member', foreignKey: 'user_id' }),
},
}
});
return Organization;
}
1条答案
按热度按时间jhdbpxl91#
问题是,您更新了一个表的关系,并且确实更改了解决方案:如果您从表中删除了记录,然后执行了ALTER操作,问题就会解决,而且不会出现序列化错误