E
E
Evgeny Petrov2020-08-12 18:55:39
Node.js
Evgeny Petrov, 2020-08-12 18:55:39

How to select in Sequelize associations not by id, but by another field?

Hello!

Please tell me, in Sequelize, the connection with another table goes through id, i.e.

//модель Status
Status.associate = function(models) {
    models.Status.hasMany(models.Orders,{
      foreignKey: 'status'
    })
  };

//модель Orders
Orders.associate = function(models) {
  models.Orders.belongsTo(models.Status,{
      foreignKey: 'status'
    })
};

Based on this code, something like this is built:
... 
LEFT OUTER JOIN `status` AS `Status` 
                    ON `orders`.`status` = `status`.`id`
...

Here, by default, the comparison goes through id, but how can I select not by id, but by another field? Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-08-12
@Nc_Soft

{ foreignKey: 'status', targetKey: '****' }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question