坚持使用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'
}]
})
答案 0 :(得分:0)
尝试使用
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