我有一个具有级联关联的模型,例如:
调查 > 问题 > 选项
调查有很多问题
问题有很多选项
创建调查时,我想创建带有选项的问题。
前对象:
survey = {
title: title,
description: description,
Questions:[
{
question_type: 'Radio',
question: 'q1',
Options:[
{
option: 'o1'
},
{
option: 'o2'
}
]
}
]
}
当我使用下面的选项创建时,出现错误。
Unhandled rejection TypeError: Cannot read property 'getTableName' of undefined
我的创作看起来像这样
models.Survey.create(survey,{
include: [models.Question,{include: [models.Option]}]
}).then(function() {
reply({success:1});
});
表模式
题:
QuestionId
SurveyId
我还有一个问题。如果我删除“选项”关联,它会插入问题,但它会在“调查”创建后输入“NULL”作为 SurveyId。
最佳答案
存在连接关联错误。尝试一下:
models.Survey.create(survey, {
include: [{
model: models.Question,
include: [models.Option]
}]
}).then(function() {
reply({success:1});
});
关于sequelize.js - 在具有关联的多个表中序列化插入(级联),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40638814/