按 belongsToMany 关系字段过滤

Posted

技术标签:

【中文标题】按 belongsToMany 关系字段过滤【英文标题】:Filter by belongsToMany relation field 【发布时间】:2015-05-20 01:27:16 【问题描述】:

无法使用链接表过滤数据。有两张桌子 Instructor 和 Club。他们关联了如何属于ToMany。我需要获取所有 club_id = value 的教练。

导师模式:

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'
            );
        
    
);

俱乐部模式:

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

相关表:

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

我正在尝试按如下方式获取数据::

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

当前查询生成的 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;

嗯,我需要一些这样的:

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;

【问题讨论】:

您是否尝试将“位置”向上移动到包含中(使用模型、as 和 through)? 事实证明。我以前试过,没用。非常感谢。 @MarkLeiber 将此添加为答案,我将其标记为正确。 【参考方案1】:

将您的“位置”向上移动到包含中(使用模型、as 和 through)。

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

【讨论】:

以上是关于按 belongsToMany 关系字段过滤的主要内容,如果未能解决你的问题,请参考以下文章

在Django中按关系字段过滤多对多关系

Laravel 按关系字段过滤项目

按 foreignKey 关系中的字段过滤

Laravel - 按“字段及其关系模型字段”之间的条件过滤模型

Laravel 6.9 belongsToMany 关系返回一个集合

按关系字段 (ACF) 的 Elementor 帖子的自定义查询过滤器