sequelize: through: {attributes: []} does not remove attributes

Setting through attributes to [] doesn’t exclude the id columns from being selected.

The model query:

componentsPromise = models.Component.findAll({
    include: [{
        attributes: [],
        model: models.Platform,
        through: {
            attributes: []
        },
        as: 'platforms',
        where: {
            id: {
                in: ids
            }
        }
    }],
    group: ['component.id'],
    having: ['COUNT(?) = ?', 'component.id', ids.length]
})

Defining the association:

Component.belongsToMany(Platform, {
    through: 'platform_components',
    as: 'platforms',
    foreignKey: 'component_id',
    otherKey: 'platform_id'
});

Platform.belongsToMany(Component, {
    through: 'platform_components',
    as: 'components',
    foreignKey: 'platform_id',
    otherKey: 'component_id'
});

The result query:

SELECT
  "component"."id",
  "component"."component_name",
  "component"."component_type",
  "component"."created_at",
  "component"."updated_at",
  "platforms.platform_components"."platform_id"  AS "platforms.platform_components.platform_id",
  "platforms.platform_components"."component_id" AS "platforms.platform_components.component_id"
FROM "components" AS "component" INNER JOIN
  ("platform_components" AS "platforms.platform_components" INNER JOIN "platforms" AS "platforms"
      ON "platforms"."id" = "platforms.platform_components"."platform_id")
    ON "component"."id" = "platforms.platform_components"."component_id" AND "platforms"."id" IN ('1', '2', '3')
GROUP BY "component"."id", "platforms.platform_components.platform_id", "platforms.platform_components.component_id"
HAVING COUNT('component.id') = 3;

This is a major problem in postgres, as this query will fail because the group by must include the through table primary keys, which subsequently makes the query not function.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 19 (7 by maintainers)

Most upvoted comments

includeIgnoreAttributes: false, solves the problem in sequelize 4.

If you have a chance to verify this behaviour, perhaps we can mark this a bug and work on a fix.

This may be a reason to have a “join” parameter. It would run a join against a table without needing the data in those tables.

Something like:

models.User.findAll({
  join: [{
    model: models.Role
    where: {
      created_at: TODAY
    }
  }]
})