Skip to content

Incorrect handling of postgres table aliases in where clauses when using non-default schema in >= 7.0.0-alpha.20 #15749

Closed

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

I am using sequelize and postgres and setting a non-default schema (i.e. not public) when initialising sequelize. When I make a find query with a where condition I am seeing that sequelize is aliasing the table (e.g. FROM "schema"."table" as "Alias") and is using that alias in the where clause. However, it is producing an invalid statement by prefixing the alias with the schema (e.g. WHERE "schema"."Alias" ...).

I can not reproduce this in 7.0.0-alpha.19. I had a brief look through the changelog and suspect that #15274 could be a candidate for introducing this regression but have not confirmed.

Reproducible Example

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

What do you expect to happen?

Where the table alias is used in the where 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 th where clause.

Environment

  • Sequelize version: >= 7.0.0-alpha.20
  • Node.js version: v18.14.1
  • Database & Version: PostgreSQL 14.3
  • Connector library & Version: [email protected]

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 Mar 8, 2023
  2. frostzt commented on Mar 9, 2023

    @frostzt
    Member

    In your code provided if I remove the schema from Sequelize init options:

    const sequelize = new Sequelize({
      dialect: "postgres",
      host: process.env.PGHOST,
      port: process.env.PGPORT,
      username: process.env.PGUSER,
      password: process.env.PGPASSWORD,
      database: process.env.PGDATABASE,
      // NO SCHEMA HERE
    });

    Now where you initialize the Entity table I passed the schema like this:

    class Entity extends Sequelize.Model {}
    Entity.init(
      {
        id: { type: DataTypes.INTEGER, primaryKey: true },
        prop: { type: DataTypes.STRING, allowNull: false },
      },
      { sequelize, tableName: "entity", schema: "dev" }
    );

    Now it generates this!

    SELECT "id", "prop", "createdAt", "updatedAt" FROM "dev"."entity" AS "Entity" WHERE "Entity"."prop" = 'hello, world' LIMIT 1;

    Now as far as the other way goes I'll look into it.

  3. ephys commented on Mar 9, 2023

    @ephys
    Member

    I wonder if this is fixed in #15598, considering it completely rewrites how WHERE generates

  4. removed
    pending-approval
    on Mar 9, 2023
  5. modified the milestone: v7 on Mar 9, 2023
  6. moved this to Before v7 in Roadmapon Mar 9, 2023
  7. chriswheeldon-peakon commented on Mar 9, 2023

    @chriswheeldon-peakon
    Author

    @ephys I have run my example against your branch locally and I no longer see the issue 🥳

    Do you think it is worth attempting to fix this in current master or better to just wait for your PR to land?

  8. ephys commented on Mar 9, 2023

    @ephys
    Member

    The PR is ready and is waiting for a review. Considering how horribly complex the old WHERE code was, I would prefer waiting for that PR to land 😅

    That being said if you or anyone already knows how to fix it, I have no problem merging an intermediary PR to fix it :)

  9. ephys commented on Mar 12, 2023

    @ephys
    Member

    #15598 has been merged :)

  10. moved this from Before v7 to Done in Roadmapon Mar 12, 2023
  11. chriswheeldon-peakon commented on Mar 15, 2023

    @chriswheeldon-peakon
    Author

    Thank you @ephys! I tested again and my now code now doesn't get as far as generating a query at all. Instead the following code from https://github.com/chriswheeldon-peakon/sequelize-v7-alias-bug gives an error when sequelize parses the where:

    class Entity extends Sequelize.Model {}
    Entity.init(
      {
        id: { type: DataTypes.INTEGER, primaryKey: true },
        prop: { type: DataTypes.TEXT, allowNull: false },
      },
      { sequelize, tableName: "entity" }
    );
    await sequelize.sync();
    
    await Entity.findOne({ where: { prop: "hello, world" } });
    BaseError [SequelizeBaseError]: Invalid value received for the "where" option. Refer to the sequelize documentation to learn which values the "where" option accepts.
    Value: { prop: 'hello, world' }
    

    Is that error expected now? I think it should work from the latest docs. Forcing prop to be cast to ::text does work.

  12. ephys commented on Mar 15, 2023

    @ephys
    Member

    The error you posted is missing details on what exactly failed, can you check what the "cause" property of the error says?

    Some testing frameworks (and older node versions) unfortunately don't print the error cause yet

    (I'm not near a computer right now but i'll run the repro once I get to one)

  13. jfow commented on Mar 15, 2023

    @jfow

    I'm also looking at this issue in the repro rep, cause printed below:

    /Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/dialects/abstract/where-sql-builder.js:158
          throw new import_base_error.BaseError(`Invalid value received for the "where" option. Refer to the sequelize documentation to learn which values the "where" option accepts.
                ^
    
    BaseError [SequelizeBaseError]: Invalid value received for the "where" option. Refer to the sequelize documentation to learn which values the "where" option accepts.
    Value: { prop: 'hello, world' }
        at WhereSqlBuilder.formatWhereOptions (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/dialects/abstract/where-sql-builder.js:158:13)
        at PostgresQueryGenerator.whereItemsQuery (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/dialects/abstract/query-generator-typescript.js:160:33)
        at PostgresQueryGenerator.selectQuery (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/dialects/abstract/query-generator.js:1059:28)
        at PostgresQueryInterface.select (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/dialects/abstract/query-interface.js:923:37)
        at Entity.findAll (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/model.js:959:47)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async Entity.findOne (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/model.js:1091:12)
        at async file:///Users/jeffrey.fowler/development/sequelize-v7-alias-bug/index.js:30:1 {
      [cause]: TypeError: Cannot read properties of undefined (reading 'getReach')
          at SyntaxNode.getReach (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/syntax.js:70:50)
          at Sequence.parseSingle (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:347:32)
          at Sequence.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:271:28)
          at Sequence.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:323:25)
          at Rule.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:389:28)
          at Parser.parse (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/bnf-parser/bin/parser.js:425:29)
          at parseAttributeSyntaxInternal (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/utils/attribute-syntax.js:97:34)
          at memoized (/Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/lodash/memoize.js:62:23)
          at /Users/jeffrey.fowler/development/sequelize-v7-alias-bug/node_modules/@sequelize/core/lib/dialects/abstract/where-sql-builder.js:215:88
          at Array.map (<anonymous>)
    }
    
  14. ephys commented on Mar 15, 2023

    @ephys
    Member

    Ok that is 100% a bug

  15. WikiRik commented on Mar 15, 2023

    @WikiRik
    Member

    Might be due to the new bnf-parser version; #15792 (do note that the changelog and GitHub releases is incorrect and ships more to NPM, see https://app.renovatebot.com/package-diff?name=bnf-parser&from=3.1.1&to=3.1.2 )

  16. ephys commented on Mar 15, 2023

    @ephys
    Member

    I was just about to say this :) Updated bnf-parser in our repo and our tests fail.
    We'll pin the dependency for now & look into this

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

    type: bug

    Type

    No type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions