Skip to content

Composite where clause using sequelize.where() #6666

Closed

Description

@jharting

What you are doing?

I am using Sequelize API where() method to construct agregate function params

attributes: [
        [sql.fn('SUM', sql.where(sql.col('system.code'), 'foo')), 'sum'];
    ]

This works as expected and generates:

SUM(`system`.`code` = 'foo')

However, when I try to use Sequelize.and() or Sequelize.or() to create a composite condition, e.g.:

[sql.fn('SUM',
    sql.and(
        sql.where(sql.col('system.code'), 'foo'),
        sql.where(sql.col('system.type'), 'bar')
    ))
    , 'sum']

I expect

SUM(`system`.`code` = 'foo' AND `system`.`type` = 'bar')

What is actually happening?

but instead I am getting:

TypeError: val.replace is not a function
    at Object.SqlString.escape (/opt/insights/api/node_modules/sequelize/lib/sql-string.js:63:15)
    at Object.QueryGenerator.escape (/opt/insights/api/node_modules/sequelize/lib/dialects/abstract/query-generator.js:983:22)
    at /opt/insights/api/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1826:23
    at Array.map (native)
    at Object.QueryGenerator.handleSequelizeMethod (/opt/insights/api/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1822:42)
    at /opt/insights/api/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1083:26
    at Array.map (native)
    at Object.QueryGenerator.selectQuery (/opt/insights/api/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1072:55)
    at QueryInterface.select (/opt/insights/api/node_modules/sequelize/lib/query-interface.js:651:25)
    at null.<anonymous> (/opt/insights/api/node_modules/sequelize/lib/model.js:1388:32)
    at processImmediate [as _immediateCallback] (timers.js:374:17)

Dialect: mysql
__Database version: 10.0.26-MariaDB MariaDB Server
__Sequelize version: 3.19.0

Is there some other way to get a composite where clause (for multiple attributes) out of Sequelize.where()?

Activity

  1. felixfbecker commented on Oct 5, 2016

    @felixfbecker
    Contributor

    What is this SQL supposed to do?

    SUM(`system`.`code` = 'foo' AND `system`.`type` = 'bar')
  2. jharting commented on Oct 5, 2016

    @jharting
    ContributorAuthor

    It defines a column that contains the count of all entities that match the condition. Might not make sense on its own but I am using multiple in combination to fetch stats:

    SELECT
        SUM(`system`.`code` = 'foo' AND `system`.`type` = 'foo') as 'foosum',
        SUM(`system`.`code` = 'bar' AND `system`.`type` = 'bar') as 'barsum',
        SUM(`system`.`code` = 'baz' AND `system`.`type` = 'baz') as 'bazsum'
    FROM ...
  3. felixfbecker commented on Oct 5, 2016

    @felixfbecker
    Contributor

    Ah, okay. I didn't know you could pass a condition to the SUM function. I think Sequelize currently does not handle a Sequelize.where() as second param to Sequelize.fn(). Feel free to do a PR.

  4. added
    type: feature
    on Oct 5, 2016
  5. jharting commented on Oct 5, 2016

    @jharting
    ContributorAuthor

    No, fn() actually works fine with where(). The problem is when I try to combine two where()s by wrapping them within and() or or().

  6. felixfbecker commented on Oct 5, 2016

    @felixfbecker
    Contributor

    and is defined as this:

    and(...conditions) {
      return {$and: conditions}
    }

    So it's basically support for plain operators as the second argument to fn().

  7. KhizarSH commented on Jul 6, 2017

    @KhizarSH

    can any one wtrite this query in sequelized form
    SELECT
    createdAt,
    COUNT(*) AS TOTAL,
    sum(DATE_FORMAT(createdAt,'%Y%c%d')=DATE_FORMAT(current_date(),'%Y%c%d')) tcount,
    sum(DATE_FORMAT(createdAt,'%Y%c%d')=DATE_FORMAT(current_date()-1,'%Y%c%d')) ycount,
    sum(DATE_FORMAT(createdAt,'%Y%c%d')=DATE_FORMAT(current_date()-2,'%Y%c%d')) threecount,
    sum(DATE_FORMAT(createdAt,'%Y%c%d')=DATE_FORMAT(current_date()-3,'%Y%c%d')) fourcount,
    sum(DATE_FORMAT(createdAt,'%Y%c%d')=DATE_FORMAT(current_date()-4,'%Y%c%d')) fifthcount
    FROM r5.users;

  8. felixfbecker commented on Jul 6, 2017

    @felixfbecker
    Contributor

    @KhizarSH please use Slack or StackOverflow for help

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: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions