【问题标题】:sequelize findOrCreate doesn't return arraysequelize findOrCreate 不返回数组
【发布时间】:2018-04-05 14:05:45
【问题描述】:

卡住了 Sequelize findOrCreate 方法。
所以我期待得到tags的空数组,但我得到tag.id, tag.name ...等。

[ { id: 2,
    name: 'Inf 1',
    email: 'andrew@ww.rt',
    createdAt: 2017-10-24T16:30:38.314Z,
    updatedAt: 2017-10-24T16:31:02.076Z,
    category_id: null,
    'tags.id': null, <---- Here is should be just like tags: []` 
    'tags.name': null,
    'tags.description': null,
    'tags.createdAt': null,
    'tags.updatedAt': null,
    'tags.UsersTags.createdAt': null,
    'tags.UsersTags.updatedAt': null,
    'tags.UsersTags.user_id': null,
    'tags.UsersTags.tag_id': null },
  false ] 

模型使用

User.findOrCreate({
    raw: true,
    where: query,
    defaults: update,
    include: [{
      model: Model.Tag,
      as: 'tags'
    }]
  })

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    尝试使用

    User.findOrCreate({
        raw: true,
        where: query,
        defaults: update,
        include: [{
          model: Model.Tag,
          as: 'tags',
          required: false // <-- thanks to this will return null if none is joined
        }]
      })
    

    参考:http://docs.sequelizejs.com/manual/tutorial/models-usage.html#nested-eager-loading

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2014-01-05
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2017-06-20
      • 2020-10-07
      相关资源
      最近更新 更多