[英]Why doesn't sequelize include associated model to a nested object in the results?
我有两个关联模型:TariffsModel 和 PoliciesModel。 PoliciesModel 具有 tariff_id,它通过其 ID 引用特定关税。 这可行,但 Sequelize 以一种奇怪的方式返回结果:而不是返回:
PoliciesModel {
id: 1,
owner_party_id: 2,
tariff: {
id: 1,
is_active: true,
...
},
...
}
它返回:
PoliciesModel {
id: 1,
owner_party_id: 2,
tariff.id: 1,
tariff.is_active: true,
...
}
这是示例:在此处输入图像描述
我这样发起关系:
const {
PoliciesModel,
TariffsModel,
} = this.sequeluze.models;
PoliciesModel.belongsTo(TariffsModel, { foreignKey: 'tariff_id', as: 'tariff' });
并像这样进行查询:
const policies = await PoliciesModel.findAll({
where: { owner_party_id: userAccount.party_id },
include: {
model: TariffsModel,
as: 'tariff',
},
raw: true,
});
这种行为的原因可能是什么?
我尝试调用 hasMany/hasOne 方法并as
参数,但没有帮助。
在您的 Sequelize 查询中,如果您删除属性raw那么您将获得预期的响应。
const policies = await PoliciesModel.findAll({
where: { owner_party_id: userAccount.party_id },
include: {
model: TariffsModel,
as: 'tariff',
}
});
查询应类似于以下查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.