postgresql - 如何从sequelize中的关联表中获取数据
问题描述
找不到让子查询与 sequelize 一起使用的方法,所以我使用了原始查询。我试图弄清楚当我进行原始查询时如何从关联表中获取数据。这是我尝试过的,但它只返回主表中的数据,而不返回关联表中的数据:
const rawQuery = `select * from (
select distinct on ("patientId") *
from public."Billings"
order by "patientId","createdAt" desc) as "recentPatientBilling"
where balance > 0;`;
const debtors = await sequelize.query(
rawQuery,
{
model: Billing,
mapToModel: true,
nest: true,
raw: true,
include: [{
model: Patient, attributes: ['id']
}]
}
);
该协会是:
Billing.associate = function(models) {
Billing.belongsTo(models.User, {
foreignKey: 'employeeId',
as: 'employee'
});
Billing.belongsTo(models.Patient, {
foreignKey: 'patientId',
as: 'patient'
});
};
关于如何让这个工作的任何想法?(已编辑)
解决方案
您是否尝试将 {as:'patient'} 放在包含语句中?
include: [{
model: Patient,
as:'patient',
attributes: ['id']
}]
推荐阅读
- javascript - Javascript(本地存储)只存储一个值。我怎样才能存储所有东西?
- google-apps-script - 如果私钥未直接存储在应用程序脚本文件中,则服务帐户的 Google Apps 脚本 OAuth2 库错误
- javascript - 如果在 Angular 超时中返回 false,则重试函数调用
- typescript - 如何导出可以从浏览器命令行访问的打字稿功能?
- ionic-framework - 带有ionic3的谷歌分析不适用于ios
- hyperledger-fabric - 如何集成 Hyperledger Fabric 网络和 Hyperledger Composer
- opencv - 为什么即使在我的树莓派 3 上安装了 openalpr 后,我也会收到 alpr 错误?
- c - 为什么 strcpy 在写入较短的数组时不会导致分段错误?
- java - 已为此响应调用 Tomcat 8.5 response.getWriter()
- android - Android如何用rows和col span实现这种类型的布局