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

Regression (5.17.2 -> 5.18.0): bug in aggregation with alias #11388

Open
2 of 7 tasks
madmoizo opened this issue Sep 5, 2019 · 8 comments
Open
2 of 7 tasks

Regression (5.17.2 -> 5.18.0): bug in aggregation with alias #11388

madmoizo opened this issue Sep 5, 2019 · 8 comments
Labels
existing workaround For issues. There is a known workaround for this issue. type: bug

Comments

@madmoizo
Copy link
Contributor

madmoizo commented Sep 5, 2019

Issue Description

Before v5.18.0, it was possible to do some aggregation using alias name directly.
(Probably related to 3fcb2d2)

Question: is there an alternative official way to do the following request without using alias explicitly?

What are you doing?

await User.findAll({
    attributes: [
      'id',
      'firstName',
      'lastName',
      'color',
      [
        Sequelize.fn('count', Sequelize.col('missions.clientfileId')),
        'totalMissions'
      ],
      [
        Sequelize.fn('sum', Sequelize.col('missions->clientfile->invoice.totalTaxed')),
        'totalRevenue'
      ]
    ],
    where: {
      officeId: state.decodedToken.officeId
    },
    include: [{
      association: 'missions',
      attributes: [],
      where: {
        startDate: {
          [Op.gte]: start,
          [Op.lte]: end
        }
      },
      include: [{
        association: 'clientfile',
        required: true,
        attributes: [],
        include: [{
          association: 'invoice',
          required: true,
          attributes: []
        }]
      }]
    }],
    order: [
      ['lastName', 'ASC'],
      ['firstName', 'ASC']
    ],
    group: [
      ['id']
    ]
  })

What do you expect to happen?

It works until 5.18.0

What is actually happening?

the request respond with column reference "id" is ambiguous but I suppose it's related to alias explicitly used in the request (which are no longer valid).

Environment

  • Sequelize version: 5.18.1
  • Node.js version: 12.x
  • Operating System: Windows

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
Copy link
Member

papb commented Sep 5, 2019

Did you enable the new { minifyAliases: true } option?

@papb papb added the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Sep 5, 2019
@madmoizo
Copy link
Contributor Author

madmoizo commented Sep 5, 2019

@papb no, I didn't change anything. If aliases are not minified by default, the issue could be a side effect of this PR. I can confirm at least it works in 5.17.2 but not in 5.18.0

@papb
Copy link
Member

papb commented Sep 5, 2019

@frlinw Yeah I thought so but just wanted to make sure. The thing is, you put Minified alias issue in the title but the issue itself is only related to minification in the sense that it was the feature which introduced the bug. I will rename it to reduce confusion.

@papb papb changed the title Minified alias issue Regression (5.17.2 -> 5.18.0): bug in aggregation with alias Sep 5, 2019
@papb
Copy link
Member

papb commented Sep 5, 2019

One more thing, you posted a lot of code, is all of that really needed to reproduce the issue? Can you shorten it? Also, you have posted a code snippet that shows the problem but is not entirely self-contained (i.e. I can't just copy-paste it and run it), please provide a SSCCE (also known as MCVE/reprex) for it, thanks!

@madmoizo
Copy link
Contributor Author

madmoizo commented Sep 5, 2019

Initially it was a question more than an issue, because I'm aware the direct use of aliases is not really a public thing so I wanted a supported way to access aliases (as I thought it was an alias issue).
I guess the issue is because of the group property, probably wrongly scoped.
I'll try to post a simple repro

@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 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 7, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
@github-actions
Copy link
Contributor

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

@madmoizo
Copy link
Contributor Author

This issue is still a thing

await User.findAll({
    attributes: [
      'id',
      [
        Sequelize.fn('sum', Sequelize.col('missions->clientfile->invoice.totalTaxed')),
        'totalRevenue'
      ]
    ],
    include: [{
      association: 'missions',
      attributes: [],
      include: [{
        association: 'clientfile',
        required: true,
        attributes: [],
        include: [{
          association: 'invoice',
          required: true,
          attributes: []
        }]
      }]
    }],
    group: [
      ['id']
    ]
  })

5.17.2

SELECT "User"."id", sum("missions->clientfile->invoice"."totalTaxed") AS "totalRevenue"
FROM "User" AS "User"
LEFT OUTER JOIN (
    "Mission" AS "missions"
    INNER JOIN "Clientfile" AS "missions->clientfile" ON "missions"."clientfileId" = "missions->clientfile"."id"
    INNER JOIN "Invoice" AS "missions->clientfile->invoice" ON "missions->clientfile"."id" = "missions->clientfile->invoice"."clientfileId" )
ON "User"."id" = "missions"."technicianId" GROUP BY "User"."id";

5.18.0 to 7.0.0-alpha.10

SELECT "User"."id", sum("missions->clientfile->invoice"."totalTaxed") AS "totalRevenue" 
FROM "User" AS "User" 
LEFT OUTER JOIN (
   "Mission" AS "missions"
   INNER JOIN "Clientfile" AS "missions->clientfile" ON "missions"."clientfileId" = "missions->clientfile"."id"
   INNER JOIN "Invoice" AS "missions->clientfile->invoice" ON "missions->clientfile"."id" = "missions->clientfile->invoice"."clientfileId"
) ON "User"."id" = "missions"."technicianId" GROUP BY "id"

error: column reference "id" is ambiguous

Dirty workaround

group: [
   ['"User"."id"']
]

@github-actions github-actions bot removed the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label May 24, 2022
@WikiRik WikiRik reopened this May 24, 2022
@WikiRik WikiRik added type: bug existing workaround For issues. There is a known workaround for this issue. and removed stale labels May 24, 2022
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. type: bug
Projects
None yet
Development

No branches or pull requests

3 participants