Skip to content

Can't exclude association's fields from select statement in sequelize (related to 3664) #10141

Closed

Description

@wl1711

What are you doing?

I am trying to exclude association's fields from select statement in sequelize, since I am doing aggregation I don't want to include them in the group by

testSummary(req, res) {
    return category.findAll({
            attributes: ['name', 'CapabilityTypes.name', [sequelize.fn('COUNT', sequelize.col('CapabilityTypes.name')), 'Count']],
            where: {
                deleted: false
            },
            include: [{
                model: capabilityType,
                attributes: [],
                include: [{
                    model: capability,
                    attributes: [],
                    through: {
                        attributes: []
                    },
                }]
            }],
            group: ['Category.name','CapabilityTypes.name'],
            raw:true
        })
        .then(summary => {
            res.status(200).json({
                summary: summary,
                success: true,
                message: "Successfully retrieved."
            })
        }).catch((error) => {
            console.log(error);
            res.status(500).send({
                success: false,
                message: "Failed to retrieve."
            })
        })
}

What do you expect to happen?

I expect attributes from the join table that highlighted in the raw select statement below NOT show up, and I don't want them in the group by

What is actually happening?

SELECT
"Category"."name",
"CapabilityTypes"."name",
COUNT("CapabilityTypes"."name") AS "Count",
"CapabilityTypes -> Capabilities -> CapabilityTypeMatrix"."capabilityId" AS "CapabilityTypes.Capabilities.CapabilityTypeMatrix.capabilityId",
"CapabilityTypes -> Capabilities -> CapabilityTypeMatrix"."capabilityTypeId" AS "CapabilityTypes.Capabilities.CapabilityTypeMatrix.capabilityTypeId"

FROM
"public"."Category" AS "Category"
LEFT OUTER JOIN
"public"."CapabilityType" AS "CapabilityTypes"
ON "Category"."id" = "CapabilityTypes"."categoryId"
LEFT OUTER JOIN
(
"public"."CapabilityTypeMatrix" AS "CapabilityTypes -> Capabilities -> CapabilityTypeMatrix"
INNER JOIN
"public"."Capability" AS "CapabilityTypes -> Capabilities"
ON "CapabilityTypes -> Capabilities"."id" = "CapabilityTypes -> Capabilities -> CapabilityTypeMatrix"."capabilityId"
)
ON "CapabilityTypes"."id" = "CapabilityTypes -> Capabilities -> CapabilityTypeMatrix"."capabilityTypeId"
WHERE
"Category"."deleted" = false
GROUP BY
"Category"."name",
"CapabilityTypes"."name";

__Dialect: postgres
__Database version: 9.4
__Sequelize version:4.38.0, also tried 4.37.10 and latest 4.41
Tested with latest release: No (If yes, specify that version)

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. wl1711 commented on Nov 8, 2018

    @wl1711
    Author

    commenting or uncommenitng the hightlighted part does make a different in the JSON result, but the raw query it genertated is exactly the same.

    testCapabilitySummary(req, res) {
        return category.findAll({
                attributes: ['name'],
                where: {
                    deleted: false
                },
                include: [{
                    model: capabilityType,
                    attributes: ['name'],
                    include: [{
                        model: capability,
                        attributes: ['name'],
                        **// through: {
                        //     attributes: []
                        // },**
                    }]
                }]
            })
            .then(capabilitySummary => {
                res.status(200).json({
                    capabilitySummary: capabilitySummary,
                    success: true,
                    message: "Successfully retrieved capability breakdown."
                })
            }).catch((error) => {
                console.log(error);
                res.status(500).send({
                    success: false,
                    message: "Failed to retrieve capability breakdown."
                })
            })
    }
    
  2. stale commented on Feb 7, 2019

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  3. hocinekhen commented on Sep 27, 2019

    @hocinekhen

    this still an issue, I use Sequelize v5.19.1, it just hides the attributes but the attributes still shown in select statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions