Sequelize adds id(PK) of main table in findAll with 'group' and currupts a query with it in posgre.
See original GitHub issueHello! I have Products model, and Vendors model. With assocation that Vendor has many products and product has one vendorId. I need filter vendors by product properties and/or vendor properties. Using postgre 9.6 and camelcase columns. Sequelize 3.30.2
Therefore object for querying:
Products.findAll({
attributes: [],
where: { type: 'service' },
include: [{model: Vendors, attributes: ['id', 'name', 'description'], as: 'vendor', required: true, where: { approved: true } }],
group: ['vendor.id']
})
Generates incorrect query (with “Products”.“id”):
SELECT “Products”.“id”, “vendor”.“id” AS “vendor.id”, “vendor”.“name” AS “vendor.name”, “vendor”.“description” AS “vendor.description” FROM “Products” AS “Products” INNER JOIN “Vendors” AS “vendor” ON “Products”.“vendorId” = “vendor”.“id” AND “vendor”.“approved” = true WHERE “Products”.“type” = ‘service’ GROUP BY “vendor”.“id”;
Query without “Products”.“id” works fine and returns vendorId`s filtered by vendor parameters (or/and product parameters):
SELECT “vendor”.“id” AS “vendor.id”, “vendor”.“name” AS “vendor.name”, “vendor”.“description” AS “vendor.description” FROM “Products” AS “Products” INNER JOIN “Vendors” AS “vendor” ON “Products”.“vendorId” = “vendor”.“id” AND “vendor”.“approved” = true WHERE “Products”.“type” = ‘service’ GROUP BY “vendor”.“id”;
Is any way to tell Sequelize to not include primary key if I don`t want it in attributes?
Thanks!
upd. workaround https://github.com/sequelize/sequelize/issues/7534#issuecomment-881615707
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:34 (2 by maintainers)
Adding
raw: true
to the main object solves this issue.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 🙂