Skip to content

Incorrect handling of pg table aliases in JOIN ON clauses when using non-default schema in 7.0.0-alpha.37 #17091

Open

Description

@chriswheeldon-peakon

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

This is essentially a duplicate of #15749 except pertaining to ON clauses rather than WHERE clauses.

I am using Sequelize and Postgres and setting a non-default schema (i.e. not public) when initialising Sequelize. When I make a find call with an include I am seeing that Sequelize is aliasing the FROM table (e.g. FROM "schema"."table" as "Alias") and is using that alias in the ON clause. However, it is producing an invalid statement by prefixing the alias with the schema (e.g. ON "schema"."Alias"."col" ...).

I can workaround the bug by removing the schema from the Sequelize constructor options and instead setting a schema when initialising each model. However, this has some drawbacks e.g. calls such as dropAllEnums use the constructor provided schema.

Reproducible Example

I have attempted to create a minimal, reproducible example at https://github.com/chriswheeldon-peakon/sequelize-v7-alias-bug/tree/510d766b06a0062b48d19e76e62134704a1a502d

What do you expect to happen?

Where the table alias is used in the on clause it should not be prefixed with the schema.

What is actually happening?

When using a non-default schema (i.e. not public) then sequelize is prefixing the alias with the schema when referring to it in the join on clause.

Environment

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 will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. added
    pending-approval
    type: bug
    on Feb 15, 2024
  2. WikiRik commented on Feb 15, 2024

    @WikiRik
    Member

    Likely due to this, so if we fix the TODO this issue should also be fixed;

    // TODO: use whereItemsQuery to generate the entire "ON" condition.
    let joinOn = `${this.quoteTable(asLeft)}.${this.quoteIdentifier(columnNameLeft)}`;

  3. removed
    pending-approval
    on Feb 15, 2024
  4. andretunes commented on Sep 26, 2024

    @andretunes

    Hello there,

    A quick workaround would be to replace the function quoteTable with quoteIdentifier. What do you think?

    Thank you

  5. added theissue type on Mar 9, 2025
  6. removed
    type: bug
    on Mar 9, 2025
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

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions