在sequalize中插入具有关联的数据不起作用?

时间:2019-03-01 13:20:53

标签: node.js sequelize.js

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

1 个答案:

答案 0 :(得分:0)

我删除了sourcekeytargetkey并添加了别名。效果很好

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'}]})