Tuesday, 2 May 2017

Join on specific columns using Sequelize.js

I am trying to do join some tables on specific columns using Sequelize.js.

So far, my code is something like:

table_1.findall({
  include: [{
    model: table_2
    attributes: ['id', 'another_id']
    include: [{
      model: table_3
      required: true
      attributes: ['time']
    }]
  }]
})

where each table's primary key is 'id'.

This seems to be equivalent to the following SQL (I am showing SELECT * for brevity, since that is not the focus of this question):

SELECT *
FROM table_1 as t1
LEFT OUTER JOIN table_2 as t2 ON t1.id = t2.t1_id
INNER JOIN table_3 as t3 ON t2.id = t3.t2_id

and I want to have something like:

SELECT *
FROM table_1 as t1
LEFT OUTER JOIN table_2 as t2 ON t1.id = t2.t1_id
INNER JOIN table_3 as t3 ON t2.another_id = t3.t2_id

Is there a way to force the join between t2 and t3 to use something either than the primary key of t2?

I have found the [options.include[].on] in the Sequelize documentation, but do not know what the syntax is for suppling my own ON condition.



via 31rhcp

No comments:

Post a Comment