简体   繁体   English

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

[英]insert data with association in sequalize not work?

hdr.hasMany(line,{foreignKey: 'hdr_uuid', sourceKey: 'uuid'}) hdr.hasMany(line,{foreignKey: 'hdr_uuid', sourceKey: 'uuid'})

line.belongsTo(hdr,{foreignKey: 'hdr_uuid', targetKey: 'uuid'}) line.belongsTo(hdr,{foreignKey: 'hdr_uuid', targetKey: 'uuid'})

hdr.create( { "name":"ch", "line":[{ "num":"01" },{ "num":"02" }] },{include:[line]}) hdr.create( { "name":"ch", "line":[{ "num":"01" },{ "num":"02" }] },{include:[line]})

i insert data using sequalize with nested object.it is not working?我使用带有嵌套对象的 sequalize 插入数据。它不起作用吗? my problem was data not inserted in line.only insert hdr table我的问题是数据没有插入行中。只插入 hdr 表

{ "name":"ch", 
  "line":[
 { "num":"01" },
 { "num":"02" }
]}

hdr and line definition in below attached image. hdr 和线定义在下面的附图中。 enter image description here在此处输入图片说明

I removed sourcekey and targetkey and added an alias name.我删除了sourcekeytargetkey并添加了一个别名。 It works fine它工作正常

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM