K
K
Kiryushka Tsisar2019-06-26 11:51:35
MySQL
Kiryushka Tsisar, 2019-06-26 11:51:35

What is the correct way to join the same table twice for the same query in sequelize?

Good day.
There is a request for documents, it has two fields (which serve as the original keys), by which you need to join the values ​​\u200b\u200bof another table (the same one). Sequelize is used, after reading the documentation it was not possible to do it the same way as there, the error still pops up: "You have used the alias status_seller in two separate associations. Aliased associations must have unique aliases."

Request.hasOne(RequestStatuses, {
        sourceKey: 'status_seller_id',
        foreignKey: 'id',
        as: 'status_seller',
      });

Request.hasMany(RequestStatuses, {
        sourceKey: 'status_buyer_id',
        foreignKey: 'id',
        as: 'status_buyer',
      });

Request.findAll({
include: [
          {
            attributes: ['id', 'name'],
            model: RequestStatuses,
            as: 'status_seller',
          },
          {
            attributes: ['id', 'name'],
            model: RequestStatuses,
            as: 'status_buyer',
          },
]
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question