【问题标题】:insert data with association in sequalize not work?在sequelize中插入具有关联的数据不起作用?
【发布时间】:2019-07-23 12:58:02
【问题描述】:

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

【问题讨论】:

  • 不清楚你在问什么。怎么了?您收到什么错误?
  • hasMany, belongsTo 用于插入数据,但不是在行表中插入数据。它只是插入 hdr 表
  • 请添加您用于插入该数据的代码。还包括用于创建这两个表的代码。
  • 您好,我已附上图片。请根据我的问题给出解决方案
  • hdr.create( { "name":"ch", "line":[{ "num":"01" },{ "num":"02" }] },{包括:[line]})

标签: node.js sequelize.js


【解决方案1】:

我删除了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'}]})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多