Join across multiple junction tables with Sequelize

前端 未结 2 1015
灰色年华
灰色年华 2021-01-31 03:17

I have a database with three primary tables: users, teams, and folders joined by two junction tables, users_teams and t

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 04:17

    Pay attention to following:

    • Define relations in both directions
    • Check you have foreignKey, otherKey in correct order
    User.belongsToMany(Team, {
      through: 'users_teams',
      foreignKey: 'user_id',
      otherKey: 'team_id'
    });
    
    Team.belongsToMany(User, {
      through: 'users_teams',
      foreignKey: 'team_id',
      otherKey: 'user_id'
    });
    

提交回复
热议问题