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

Can't use array of sequelize.where() on having or where properties of the options object #10142

Open
lucastaliberti opened this issue Nov 9, 2018 · 9 comments
Labels
status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug

Comments

@lucastaliberti
Copy link

What are you doing?

I'm trying to build a query with multiple having filters, like the code below

Patient.findAndCountAll({
    attributes: [
      [
        sequelize.fn('sum', sequelize.col('deliveredProcedures.totalAmount')),
        'totalAmount',
      ],
      [
        sequelize.fn('count', sequelize.col('deliveredProcedures.id')),
        'proceduresCount',
      ],
    ],
    include: [
      {
        model: DeliveredProcedure,
        as: 'deliveredProcedures',
        where: {
          entryDate: {
            gt: moment()
              .subtract(1, 'years')
              .toISOString(),
            lt: new Date(),
          },
        },
        attributes: [],
        duplicating: false,
        required: true,
      },
    ],
    group: ['Patient.id'],
    having: [
      sequelize.where(sequelize.fn('sum', sequelize.col('totalAmount')), {
        gte: minValue,
        lte: maxValue,
      }),
      sequelize.where(sequelize.fn('count', sequelize.col('deliveredProcedures.id')), {
        gte: minValue,
        lte: maxValue,
      })],
  });

This code will throw
[ERROR] : Support for literal replacements in the where object has been removed.

but if I add an empty object to the having property it works perfectly, which seems weird to me.

  having: [
	  {},
      sequelize.where(sequelize.fn('sum', sequelize.col('totalAmount')), {
        gte: minValue,
        lte: maxValue,
      }),
      sequelize.where(sequelize.fn('count', sequelize.col('deliveredProcedures.id')), {
        gte: minValue,
        lte: maxValue,
      })],

This is probably caused by the fact the Utils.canTreatArrayAsAnd(value) returns false to arrays that don't contain any plain object, which I'm afraid is not correct since the functionality works if I "cheat" the canTreatArrayAsAnd check.

What do you expect to happen?

Query works and having clause is properly built when passing an array of sequelize.where objects to the having or where properties of the options object.

What is actually happening?

get [ERROR] : Support for literal replacements in the where object has been removed.

Dialect: postgres / any(?)
Database version: postgres 9.6
__Sequelize version: 4.4
__Tested with latest release: No

@maiquelleonel
Copy link

I was going to report the same problem but your trick on clause having saved my day 😄

Thanks a lot!

@RafeOO
Copy link

RafeOO commented Mar 7, 2019

Your trick on clause having saved my day.

Thanks

@MTodorov94
Copy link

Save my day too :D

@stale stale bot added the stale label Jul 20, 2019
@papb papb self-assigned this Jul 20, 2019
@stale stale bot removed the stale label Jul 20, 2019
@sequelize sequelize deleted a comment from stale bot Jul 26, 2019
@papb
Copy link
Member

papb commented Jul 26, 2019

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). Can you (or someone else) please provide a SSCCE (also known as MCVE/reprex)? This will help tackling the issue faster.

@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug labels Jul 26, 2019
@lucastaliberti
Copy link
Author

Hey @papb sorry about that. Do you have any recomendation on how to that properly ? I can only think of mocking the database, if that is what you have in mind. I can do it.

@papb
Copy link
Member

papb commented Aug 6, 2019

@lucastaliberti I just want a code that I can copy-paste and run and see the problem. If I try to run the code in your first post, I will obviously get a Patient is not defined error in the first line. So just provide a model definition, and create() some instances if necessary.

In short: just put yourself in my shoes trying to reproduce your problem with the least effort as possible 😬 imagine what I will do: create an empty folder, install sequelize, and run your code...

Shouldn't take too long 👍

@lucastaliberti
Copy link
Author

Hey, @papb sorry for the delay.
Here is an MCVE in codesandbox. Please let me know if you need anything else.

https://codesandbox.io/s/sequelizeissue10142-vqozc

@murongg
Copy link

murongg commented Mar 10, 2020

Has this issue been resolved?

@lucastaliberti
Copy link
Author

@papb did you have the chance to check my example? I believe we could remove the awaiting response tag by now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug
Projects
None yet
Development

No branches or pull requests

6 participants