首页 > 解决方案 > Sequelizejs 通过两个字段“包含”

问题描述

我正在尝试将 2 个表与 sequelizejs 相关联。

表 1 具有用于关系的 ID 字段,表 2 具有用于关系的 2 个字段,一个是模块(字符串)和关系的 ID。

表 1:id order_id ...其他字段

表2:id模块relation_id'''其他字段

SQL 语法如下:

SELECT * FROM table1 AS t1 
LEFT JOIN table2 AS t2 ON t1.order_id = t2.relation_id AND 'SAC' = t2.module
INNER JOIN table3 AS t3 ON t1.c_id = t3.id
WHERE t1.active = 1

sequelize 语法如下:

table1.findAll({
include: [
  { 
    model: sequelize.models.table3
  },
  {
    model: sequelize.models.table2,
    as: 't2',
    on: { 
        'SAC' : {$col: 't2.module'},
        table1.order_id: {$col: 't2.relation_id'}
    }
  }
]
})
.then((result) => {
    ...
})
.catch((err) => {
    ...
});

但是 sequelize 给了我下一个错误:

Error => { SequelizeEagerLoadingError: t2 is not associated to table1!

我感谢你的帮助

标签: javascriptmysqlnode.jssequelize.js

解决方案


推荐阅读