【问题标题】:Filter by belongsToMany relation field按 belongsToMany 关系字段过滤
【发布时间】: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 将此添加为答案,我将其标记为正确。

标签: sql node.js orm sequelize.js


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2021-06-12
    • 2023-03-06
    • 2019-10-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多