I have 3 models User, Question and Video
- A video belongs to one user and to also one question
- user has many questions, while a question has only 1 video
- I have got the association between user and video models done, however for video-question one not yet. The foreign keys are created successfully but on create method the questionId is not assigned.
Hers is a snap shot of how the models and the create function looks like. Video Model
'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;};
Question model:
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;};
For the create function
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));
}
The video is created however it neglects completely the existence of questionId.