【发布时间】:2020-12-13 07:59:01
【问题描述】:
问题:
我无法使用 Sequelize@5.21.2 检索包含内的包含。 第一个包括它的确定。但是当我尝试获取其他包含时,我只收到 null。
代码:
let planCodes = ['monthly', 'annual'];
const plans = await Plan.findAll({
where: {
code: {
[Op.or]: planCodes
}
},
include: [{
model: PlanItem,
as: 'plan_items',
include: [{
model: Product,
as: 'product',
}]
}]
});
模型关联:
计划
Plan.hasMany(PlanItem, { foreignKey: "plan_id" });
计划项目
PlanItem.hasOne(Product, { foreignKey: "plan_item_id" });
回应:
[
{
"id": 175231,
"name": "mensal",
"interval": "months",
"interval_count": 1,
"code": "monthly",
"status": "active",
"metadata": {},
"created_at": "XXXX-XX-XX",
"updated_at": "XXXX-XX-XX",
"plan_items": [
{
"id": 190651,
"plan_id": 175231,
"product_id": 655939,
"created_at": "XXXX-XX-XX",
"updated_at": "XXXX-XX-XX",
"product": null
}
]
}
]
我已经用数据库中的 ID 检查了所有详细信息,一切正常。
【问题讨论】:
-
同时显示模型定义
标签: javascript node.js typescript sequelize.js