mysql - Foreign key is not assigned using sequelize in 1:1 association -
i have 3 models user, question , video
- a video belongs 1 user , 1 question
- user has many questions, while question has 1 video
- i have got association between user , video models done, video-question 1 not yet. foreign keys created on create method questionid not assigned.
hers snap shot of how models , 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 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 created neglects existence of questionid.
okay have solved it, problem have add hasone association in question model
Comments
Post a Comment