Repository navigation
Closed
Labels
type: featureDEPRECATED: replace with the "feature" issue type
Description
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()?
Metadata
Metadata
Assignees
Labels
type: featureDEPRECATED: replace with the "feature" issue type
Activity
felixfbecker commentedon Oct 5, 2016
What is this SQL supposed to do?
jharting commentedon Oct 5, 2016
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:
felixfbecker commentedon Oct 5, 2016
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.
jharting commentedon Oct 5, 2016
No,
fn()actually works fine withwhere(). The problem is when I try to combine twowhere()s by wrapping them withinand()oror().felixfbecker commentedon Oct 5, 2016
andis defined as this:So it's basically support for plain operators as the second argument to
fn().support condition objects in utility functions sequelize#6666
support condition objects in utility functions sequelize#6666
support condition objects in utility functions sequelize#6666
support condition objects in utility functions sequelize#6666
support condition objects in utility functions sequelize#6666
support condition objects in utility functions sequelize#6666
support condition objects in utility functions sequelize#6666
support condition objects in utility functions sequelize#6666
Support condition objects in utility functions sequelize#6666
Support condition objects in utility functions #6666 (#6685)
Support condition objects in utility functions #6666 (#6685)
KhizarSH commentedon Jul 6, 2017
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;
felixfbecker commentedon Jul 6, 2017
@KhizarSH please use Slack or StackOverflow for help