postgresql - 如何从 Sequelize 中的关联表中获取数据

标签 postgresql sequelize.js

找不到让子查询与 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 语句中?

 include: [{
        model: Patient, 
        as:'patient',
      attributes: ['id']
      }]

关于postgresql - 如何从 Sequelize 中的关联表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65073725/

相关文章:

django - Django 模型实现时间表(调度)功能的架构

spring JDBC 无法连接到 postgres 数据库,但普通 JDBC 可以连接

postgresql - Postgres : Not-null constraint with two foreign keys

javascript - 无法从单个文件导出多个 Sequelize 模型

sql - 使用 Node.js 连接表的 GraphQL 查询

javascript - 如何在javascript中将字符串对象转换为json对象

php - Postgresql 搜索早于阈值的项目

sql - 如何在 PostgreSQL 查询中声明变量

node.js - Sequelize -cli db :seed returning 'Unhandled rejection TypeError: undefined is not a function'

node.js - Sequelize插入连接表(多对多)