Sequelize, problem getting associations to return

后端 未结 4 1718
旧时难觅i
旧时难觅i 2021-02-15 13:20

I\'m currently experimenting with Sequelize and have two objects, a Person and Position, When getting a list of persons I want to get their position. <

4条回答
  •  猫巷女王i
    2021-02-15 13:40

    From April 2013 in v1.7.0 you need just to expand your Associations to:

    Position.hasMany(User, {foreignKey : 'position_id', as: 'User'});
    User.belongsTo(Position, {foreignKey : 'position_id', as: 'Posit'});
    

    and then find all Users with associated Positions

    User.findAll({
          include: [{
            model: Position, as: 'Posit'
          }]
    }).success(function(match) {
        // your logic
    });
    

提交回复
热议问题