Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specifying through.attributes does not filter columns in through tables #11819

Open
2 of 7 tasks
chengluyu opened this issue Jan 12, 2020 · 3 comments
Open
2 of 7 tasks
Labels
existing workaround For issues. There is a known workaround for this issue. status: understood For issues. Applied when the issue is understood / reproducible. type: bug

Comments

@chengluyu
Copy link

chengluyu commented Jan 12, 2020

Issue Description

What are you doing?

Let's say we have three tables: users, tasks and participation. There's a n:m relation between users and tasks, which is based on the through table participations.

The through table has many columns. In some scenarios, only one of them is needed. So I write some query like this:

User.findAll({
  include: [Task.associations.tasks],
  through: {
    attributes: ['role']
  }
})

But the query result still contains all columns. I also found this issue: #5590. But includeIgnoreAttributes seems doesn't work at all.

Here is the link to the SSCCE for this issue: sequelize/sequelize-sscce#33

What do you expect to happen?

The generated SQL query should only contain column role in table participations.

What is actually happening?

The generated SQL query contains all columns in table participations. A direct link to this line in CI output. Below is a formatted one.

SELECT
    `users`.`id`,
    `users`.`username`,
    `users`.`createdAt`,
    `users`.`updatedAt`,
    `tasks`.`id` AS `tasks.id`,
    `tasks`.`name` AS `tasks.name`,
    `tasks`.`description` AS `tasks.description`,
    `tasks`.`createdAt` AS `tasks.createdAt`,
    `tasks`.`updatedAt` AS `tasks.updatedAt`,
    `tasks->participates`.`id` AS `tasks.participates.id`, -- this line shouldn't exist
    `tasks->participates`.`role` AS `tasks.participates.role`,
    `tasks->participates`.`createdAt` AS `tasks.participates.createdAt`, -- this line shouldn't exist
    `tasks->participates`.`updatedAt` AS `tasks.participates.updatedAt`, -- this line shouldn't exist
    `tasks->participates`.`userId` AS `tasks.participates.userId`, -- this line shouldn't exist
    `tasks->participates`.`taskId` AS `tasks.participates.taskId` -- this line shouldn't exist
FROM
    `users` AS `users`
    LEFT OUTER JOIN `participates` AS `tasks->participates` ON `users`.`id` = `tasks->participates`.`taskId`
    LEFT OUTER JOIN `tasks` AS `tasks` ON `tasks`.`id` = `tasks->participates`.`userId`;

Additional context

Add any other context or screenshots about the feature request? Nope.

Environment

  • Sequelize version: sequelize@5.21.3
  • Node.js version: v13.5.0
  • Operating System: macOS Catalina 10.15.2 (19C57)
  • TypeScript: not related.

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@papb papb added P1: important For issues and PRs. status: understood For issues. Applied when the issue is understood / reproducible. type: bug labels Jan 18, 2020
@theRichu
Copy link

Hello, I found in query-generator.js

    const throughAttributes = through.attributes.map(attr => {
    const throughAttributes = (through.originalAttributes || through.attributes).map(attr => {

I checked it works, but not well tested

I wish it helps

@papb
Copy link
Member

papb commented Jan 23, 2020

See sequelize/website#97

@papb papb added existing workaround For issues. There is a known workaround for this issue. and removed P1: important For issues and PRs. labels Jan 23, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 3, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
existing workaround For issues. There is a known workaround for this issue. status: understood For issues. Applied when the issue is understood / reproducible. type: bug
Projects
None yet
Development

No branches or pull requests

4 participants