简体   繁体   中英

Filter by belongsToMany relation field

It is impossible to filter data using a linked table. There are two tables Instructor and Club. They related how belongsToMany. I need to get all Instructors which club_id = value.

Instructor model:

sequelize.define('Instructor', {
    instance_id: DataTypes.INTEGER,
    name: DataTypes.STRING(255)
}, {
    tableName: 'instructors',
    timestamps: false,
    classMethods: {
        associate: function (models) {
            Instructor.belongsToMany(models.Club, {
                through: 'InstructorClub'
            });
        }
    }
});

Club model:

sequelize.define('Club', {
    instance_id: DataTypes.INTEGER,
    name: DataTypes.STRING
}, {
    tableName: 'clubs',
    timestamps: false,
    classMethods: {
        associate: function (models) {
            Club.belongsToMany(models.Instructor, {
                through: 'InstructorClub'
            });
        }
    }
});

Related table:

sequelize.define('InstructorClub', {
    InstructorId: {
        type: DataTypes.INTEGER,
        field: 'instructor_id'
    },
    ClubId: {
        type: DataTypes.INTEGER,
        field: 'club_id'
    }
}, {
    tableName: 'instructors_clubs'
    timestamps: false
});

I am trying to get the data as follows::

models
.Instructor
.findAll({
    include: [
        {
            model: models.Club,
            as: 'Clubs',
            through: {
                attributes: []
            }
        }
    ],
    # I need to filter by club.id
    where: {
        'Clubs.id': 10
    }
})

Current query generated SQL:

SELECT  `Instructor`.`id`, 
    `Instructor`.`instance_id`, 
    `Instructor`.`name`, 
    `Clubs`.`id` AS `Clubs.id`, 
    `Clubs`.`name` AS `Clubs.name`, 
    `Clubs.InstructorClub`.`club_id` AS `Clubs.InstructorClub.ClubId`, 
    `Clubs.InstructorClub`.`instructor_id` AS `Clubs.InstructorClub.InstructorId` 
FROM `instructors` AS `Instructor` 
LEFT OUTER JOIN (`instructors_clubs` AS `Clubs.InstructorClub` INNER JOIN `clubs` AS `Clubs` ON `Clubs`.`id` = `Clubs.InstructorClub`.`club_id`) 
ON `Instructor`.`id` = `Clubs.InstructorClub`.`instructor_id` 
WHERE `Instructor`.`Clubs.id` = 10;

Well, I need some kind of this:

SELECT  `Instructor`.`id`, 
    `Instructor`.`instance_id`, 
    `Instructor`.`name`, 
    `Clubs`.`id` AS `Clubs.id`, 
    `Clubs`.`name` AS `Clubs.name`, 
    `Clubs.InstructorClub`.`club_id` AS `Clubs.InstructorClub.ClubId`, 
    `Clubs.InstructorClub`.`instructor_id` AS `Clubs.InstructorClub.InstructorId` 
FROM `instructors` AS `Instructor` 
LEFT OUTER JOIN (`instructors_clubs` AS `Clubs.InstructorClub` INNER JOIN `clubs` AS `Clubs` ON `Clubs`.`id` = `Clubs.InstructorClub`.`club_id`) 
ON `Instructor`.`id` = `Clubs.InstructorClub`.`instructor_id` 
# It should be like this:
WHERE `Clubs`.`id` = 10;

Move your 'where' up into the include (with model, as, and through).

include: [ { 
      model: models.Club, 
      as: 'Clubs', 
      through: { attributes: [] },
      where: { 'Clubs.id': 10 }
} ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM