hdr.hasMany(line,{foreignKey:'hdr_uuid',sourceKey:'uuid'})
line.belongsTo(hdr,{foreignKey:'hdr_uuid',targetKey:'uuid'})
hdr.create( {“ name”:“ ch”,“ line”:[{“ num”:“ 01”},{“ num”:“ 02”}] },{include:[line]})
我使用带有嵌套对象的sequalize插入数据。它不起作用吗? 我的问题是没有将数据插入行中。仅插入hdr表
{ "name":"ch",
"line":[
{ "num":"01" },
{ "num":"02" }
]}
hdr和下面的图像中的行定义。 enter image description here
答案 0 :(得分:0)
我删除了sourcekey
和targetkey
并添加了别名。效果很好
hdr.hasMany(line,{foreignKey: 'hdr_uuid',as:'line'})
line.belongsTo(hdr,{foreignKey: 'hdr_uuid',as:'hdr'})
hdr.create( { "name":"ch", "line":[{ "num":"01" },{ "num":"02" }] },{include:[{model:line,as:'line'}]})