Is it possible to filter a query by the attributes in the association table with sequelize?

前端 未结 2 402
渐次进展
渐次进展 2020-12-24 08:24

I am trying to filter my query by the attributes of the joining table

I have 2 tables Cities and Categories which I am associating through a third table CityCategory

相关标签:
2条回答
  • 2020-12-24 08:35

    When querying the through table, you should use through.where

    include: [{
      model: Category,
      through: { where: {year: 2015}},
      attributes: ['id']
    }]
    

    You might want to add required: true to turn the include to an inner join

    0 讨论(0)
  • 2020-12-24 08:58

    For Sequelize v3 it appears the syntax is closer to what you suggested, that is:

    include: [{
      model: Category,
      where: {year: 2015},
      attributes: ['id']
    }]
    
    0 讨论(0)
提交回复
热议问题