Skip to content

Column references are broken when used in IncludeOptions.on #14114

Open

Description

@eugenestepaniuk

[✔] I have read the contribution guidelines

Bug Description

I'm trying to use [Op.or] to execute a query

User.findOne({
      where: { email: payload.email.trim(), '$accesses.isAccess$': 1 },
      include: [{
        model: Access,
        as: 'accesses',
        on: {
          [Op.or]: [{'$User.web_roles_id$': Sequelize.col('accesses.web_roles_id')}, {'$User.id$': Sequelize.col('accesses.web_users_id')}]
        },
        include: [{
          model: Route,
          as: 'routes'
        }]
      }]
});

, but it doesn't work correctly, for some reason it throws out the fields '$User.web_roles_id$' and '$User.id$' that I want to use in comparison.

What do you expect to happen?

That should run the following query:

SELECT ... FROM [web_users] AS [User] 
LEFT OUTER JOIN [web_access] AS [accesses] ON[User].[web_roles_id] = [accesses].[web_roles_id] or [User].[id] = [accesses].[web_users_id]
LEFT OUTER JOIN [web_route] AS [accesses->routes] ON [accesses].[web_route_id] = [accesses->routes].[id] 
WHERE [User].[email] = N'[email protected]' AND [accesses].[isAccess] = 1;

What is actually happening?

It is running this instead: (fields '$User.web_roles_id$' and '$User.id$' not in operation)

SELECT ... FROM [web_users] AS [User] 
LEFT OUTER JOIN [web_access] AS [accesses] ON ([accesses].[web_roles_id] OR [accesses].[web_users_id]) 
LEFT OUTER JOIN [web_route] AS [accesses->routes] ON [accesses].[web_route_id] = [accesses->routes].[id] 
WHERE [User].[email] = N'[email protected]' AND [accesses].[isAccess] = 1;

Additional context

My associations:

User.hasMany(Access, { foreignKey: "roleId", sourceKey: "roleId", as: "accesses" });
Access.belongsTo(User, { foreignKey: "roleId", targetKey: "roleId", as: "user" });
Access.hasMany(Route, { foreignKey: "id", sourceKey: "routeId", as: "routes" });
Route.belongsTo(Access, { foreignKey: "id", targetKey: "routeId", as: "accesses" });

Environment

  • Sequelize version: 6.13.0
  • Node.js version: 16.4.2
  • If TypeScript related: TypeScript version: 4.5.4

Bug Report 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.

Activity

  1. added
    type: bug
    on Feb 16, 2022
  2. WikiRik commented on Feb 16, 2022

    @WikiRik
    Member

    Thanks for the bug report! It looks like this is something that should work. Can you make an SSCCE or failing test(s) in a PR for this issue so we can look into this?

  3. eugenestepaniuk commented on Feb 17, 2022

    @eugenestepaniuk
    Author
  4. eugenestepaniuk commented on Feb 23, 2022

    @eugenestepaniuk
    Author

    @WikiRik
    please tell me when i will get an answer to my problem or how can i know the solution?

  5. ephys commented on Feb 23, 2022

    @ephys
    Member

    The problem isn't fixed yet but I found a workaround that works: use where() instead of the POJO syntax.
    Something like this:

    import { where, col } from 'sequelize';
    
    User.findOne({
      where: { email: 'test' },
      include: [{
        association: User.associations.accesses,
    
        // this is the relevant part:
        on: {
          [Op.or]: [
            where(col('User.web_roles_id'), col('accesses.web_roles_id')),
            where(col('User.is'), col('accesses.web_users_id')),
          ],
        },
      }],
    });

    I hope #14020 will fix the underlying issue

  6. added
    existing workaround
    on Feb 23, 2022
  7. WikiRik commented on Feb 23, 2022

    @WikiRik
    Member

    Thanks for looking into this @ephys
    Since there is a workaround that's not too much work I think it's best to just leave this for now and fix it with #14020 for the v7 release.

    @eugenestepanyuk please confirm if the workaround did actually fix this issue for you

  8. self-assigned this
    on Feb 23, 2022
  9. eugenestepaniuk commented on Feb 23, 2022

    @eugenestepaniuk
    Author

    yes, thx, this workaround works
    now i found one more workaround

    import { Op, col } from "sequelize";
    
    User.findOne({
        where: { email: payload.email.trim(), '$accesses.isAccess$': 1 },
        include: [{
          model: Access,
          as: 'accesses',
          on: {
            [Op.or]: [
              { '$User.web_roles_id$': { [Op.eq]: col('accesses.web_roles_id') } },
              { '$User.id$': { [Op.eq]: col('accesses.web_users_id') } }
            ]
          },
          include: [{
            model: Route,
            as: 'routes'
          }]
        }]
    });
    

    I will hope that you will be able to solve the problem without auxiliary properties

  10. changed the title [-]Incorrect operation of the Op.or[/-] [+]Column references are broken when used in `IncludeOptions.on`[/+] on Apr 16, 2022
  11. added theissue type on Mar 9, 2025
  12. 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

Labels

existing workaround

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions