坚持使用 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'
}]
})
最佳答案
尝试使用
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
关于javascript - sequelize findOrCreate 不返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46916223/