Tag: 模型关联

模型与Model Sequelize没有关联

我试图在表格之间build立一对多的关系: Exam和Exam_Questions ,使用Sequelize。 即使表格创build正确,我可以看到他们在PhpMyAdmin,我不断收到在控制台中的以下错误: Error: exam_question is not associated to exam! exam.js … const ExamQuestion = require('./exam-question'); … const Exam = sequelizeInstance.define("exam", { name: { type: Sequelize.STRING }, date: { type: Sequelize.DATE } }); // Build the model relations Exam.hasMany(ExamQuestion, { as: "Questions" }); exam-question.js const ExamQuestion = Sequelize.db.define("exam_question", { correct_answer: { type: Sequelize.STRING }, […]

如何在Sequelize模型上实现“回复”和“回复”关联

我有一个模式在sequelize的职位。 我希望你可以检索对post的回复,以及post作为回复的post。 理论上这只需要一个外键,一个“replyId”字段,所以你有这个表格: ———————- |id|post |replyId| ———————- |1 |post one |null | |2 |replying |1 | |3 |replying |1 | ———————- 所以要获得post回复1 ,你需要查找1 replyId , 并得到什么后3回复,你看id为1 序列化关系是: Post.hasMany(models.Post, { as: 'Replies' }) Post.hasOne(models.Post, { as: 'ReplyingTo' }) 然后当添加post到数据库: //Having created `post` post.setReplyingTo(replyingToPost) //Having found `replyingToPost` replyingToPost.addReplies(post) 但是无论我尝试有什么样的错误,例如在上面的表中,它是null你得到ID 3和sequelize不会返回任何答复ID 2