我有3个模型用户,问题和视频
Hers是模型和创建函数外观的快照。 视频模型
'use strict';
module.exports = (sequelize, DataTypes) => {
var Video = sequelize.define('Video', {
transcription: {
type: DataTypes.TEXT
},
});
Video.associate = (models) => {
Video.belongsTo (models.Question,
{foreignKeyContraint : true , foreignKey: "questionId" });
}
Video.associate = (models) => {
Video.belongsTo(models.User, {
onDelete: "CASCADE",
foreignKey: 'userId'
});
}
return Video;};
问题模型:
module.exports = (sequelize, DataTypes) => {
var Question = sequelize.define('Question', {
text: {
type: DataTypes.TEXT,
allowNull: false,
}
});
Question.associate = (models) => {
Question.belongsTo(models.Script, {
onDelete: "CASCADE",
foreignKey: 'scriptId'
});
}
Question.associate = (models) => {
Question.hasMany(models.Questionvariation,
{
foreignKey: 'questionId',
as: 'questions',
});
} return Question;};
对于创建功能
create(req, res) {
return Video
.create({
userId: req.body.user_id,
questionId: req.body.question_id,
transcription: req.body.transcription
})
.then(video =>
res.status(201).send(video))
.catch(error =>
res.status(400).send(error));
}
视频已创建,但它完全忽略了questionId的存在。
答案 0 :(得分:0)
好的我已经解决了,问题是我必须在问题模型中添加一个hasOne关联