Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 496
31rhcp Avatar asked Mar 16 '26 14:03

31rhcp


2 Answers

You want to define the special case foreign key name in the associations themselves, defined in the classMethods section of your models. Here's an example of a table that joins to the users table twice, with specially named foreign keys:

classMethods: {
  associate(models) {
    this.belongsTo(models.user, {
      foreignKey: 'created_by_user_id',
      as: 'created_by',
    });
    this.belongsTo(models.user, {
      foreignKey: 'updated_by_user_id',
      as: 'updated_by',
    });
  },
},

Then, in your findAll, there's no need to do anything special - sequelize will know what foreign key column to use automatically.

like image 123
joshua.paling Avatar answered Mar 19 '26 04:03

joshua.paling


You can also mention like this in you code/controller file:

const storeModel = model.store;
const bookedTicketModel = model.booked_ticket;
bookedTicketModel.belongsTo (storeModel, {foreignKey: 'storeId'});
storeModel.hasMany (bookedTicketModel, {foreignKey: 'id'});
like image 41
Jaskaran Singh Avatar answered Mar 19 '26 02:03

Jaskaran Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!