question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sequelize adds id(PK) of main table in findAll with 'group' and currupts a query with it in posgre.

See original GitHub issue

Hello! 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:open
  • Created 6 years ago
  • Reactions:5
  • Comments:34 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
anilkonsalcommented, Oct 3, 2017

Adding raw: true to the main object solves this issue.

19reactions
stale[bot]commented, Aug 30, 2017

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 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced M:N Associations | Sequelize
This automatically added the columns userId and profileId to the Grant model. Note: As shown above, we have chosen to force the grant...
Read more >
Using group by and joins in sequelize - Stack Overflow
I have two tables on a PostgreSQL database, ...
Read more >
Four Sequelize Associations You Should Know
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, ... Joins in SQL are used to combine two or more tables in the...
Read more >
How To Use Sequelize with Node.js and MySQL - DigitalOcean
Sequelize is a Node.js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more ...
Read more >
Id column in query when not selecting it, causing problems ...
Sequelize adds the primary keys so it can do deduplication of cartesian products. `raw: true` disables this so when you can you should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found