Sequelizejs include on relating to several fields

0

I'm trying to relate some tables with sequelizejs.

Table 1 has a field with an ID that is used for the relationship and table 2 has 2 fields for the relationship, one is module (string) and the ID for the relationship.

The MySQL syntax would be as follows:

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

What I have put together in Nodejs with Sequelize is the following:

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) => {
    ...
});

But sequelize returns the following error:

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

I already thank you for your help

    
asked by Ezequiel Cohort 10.09.2018 в 18:01
source

0 answers