我正在尝试加载两个相互关联的模型。问题是,当使用与文档类似的范围时,它是必需的包含
我的场景是使用具有多个关联的 Product
模型的 Study
模型。
有多个 Study
模型尚未具有任何 Product
关联(它们最初并未创建)。
我的范围定义如下:
const Study = sequelize.define(
'study',
...
scopes: {
withProducts: {
include: [
{
model: Product
}
]
},
有 20 个 Study
条目,但以下仅返回 14 个,因为其中 6 个 Study
条目没有任何 Product
关系。
const allStudies = await Study.scope('withProducts').findAll()
非常感谢任何帮助!
最佳答案
在这种情况下,您必须显式设置required: false
const Study = sequelize.define(
'study',
...
scopes: {
withProducts: {
include: [
{
required: false, // HERE
model: Product
}
]
},
关于node.js - Sequelize - 急切加载与可选范围的关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52063685/