You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
(Aliased) Count in raw query returns string instead of integer for the count column #2383
Just tested it with count(id) without an alias as well and it still returns the count as a string. I'm sure that max, min and avg functions would have the same effect.
What dialect? It's likely a dialect connector issue rather than a sequelize issue. Postgres returns them as strings because of a large number issue in javascript afair.
Sorry, I should have mentioned that in the OP. Using Postgres. Can you recommend a fix that doesn't involve having to iterate over the results and parse the value, or do you think that's the only solution?
You're right, it's not a Sequelize issue. :)
Solved by making a small change in the file lib/dialects/postgres/connector-manager.js
I commented out the line:
this.pg.types.setTypeParser(20, String);
And changed it to:
this.pg.types.setTypeParser(20, 'text', parseInt)
@Roam-Cooper yeah i recall seeing that before, it's not something we can generaly fix in Sequelize but perhaps it's something we should expose so people could call it easily without changing the sequelize code.
Yeah, it'd be great to have a setting in Sequelize for it as my modified files won't really work as we push our app to AWS EB, which installs fresh modules for us (so not including my changes).
I would have preferred that the PG guys keep it as int and then convert to string only when the value surpasses the postive value threshold for JS integers.
In my models file? Could you please clarify? Because I only ever require Sequelize, I don't believe I have access to the pg that Sequelize uses unless I modify Sequelize code, which I can not do.
pg exports a singleton object, so require('pg') in your code will return the same object that sequelize has access to. I just tested it, and doing require('pg').defaults.parseInt8 = true outside of sequelize code works fine
Reacted by Philographer, Galin, Jia Yin, Shawn Bissell, Dan Prince, Minjun Kim, Greg, Mike, Bernardo da Eira Duarte, Nate and 22 more
Reacted by Fernando de Paula F. Maciel, hoffmannpaola and Harrison Mitchell
Reacted by Dmitriy Pushkov, Kevin Ashcraft, Sweta Unagar, Guillaume TRUPIN, Tarjei Holtskog, Fernando de Paula F. Maciel, Anton, hoffmannpaola, Harrison Mitchell and s4nt14go
Reacted by Gleb Gladkiy, Pedro Machado, Tarjei Holtskog, Fernando de Paula F. Maciel, hoffmannpaola, Harrison Mitchell, Andrei and Aymen ADJLANE
Reacted by Abhishek Shah, Fernando de Paula F. Maciel, hoffmannpaola and Harrison Mitchell
Activity
Roam-Cooper commentedon Oct 2, 2014
Just tested it with count(id) without an alias as well and it still returns the count as a string. I'm sure that max, min and avg functions would have the same effect.
mickhansen commentedon Oct 3, 2014
What dialect? It's likely a dialect connector issue rather than a sequelize issue. Postgres returns them as strings because of a large number issue in javascript afair.
Roam-Cooper commentedon Oct 4, 2014
Sorry, I should have mentioned that in the OP. Using Postgres. Can you recommend a fix that doesn't involve having to iterate over the results and parse the value, or do you think that's the only solution?
You're right, it's not a Sequelize issue. :)
Solved by making a small change in the file lib/dialects/postgres/connector-manager.js
I commented out the line:
this.pg.types.setTypeParser(20, String);
And changed it to:
this.pg.types.setTypeParser(20, 'text', parseInt)
As mentioned in brianc/node-postgres#427
mickhansen commentedon Oct 4, 2014
@Roam-Cooper yeah i recall seeing that before, it's not something we can generaly fix in Sequelize but perhaps it's something we should expose so people could call it easily without changing the sequelize code.
Roam-Cooper commentedon Oct 5, 2014
Yeah, it'd be great to have a setting in Sequelize for it as my modified files won't really work as we push our app to AWS EB, which installs fresh modules for us (so not including my changes).
I would have preferred that the PG guys keep it as int and then convert to string only when the value surpasses the postive value threshold for JS integers.
janmeier commentedon Oct 5, 2014
In your own models file you should be able to require pg and do the changes mentioned in the issue you linked
Roam-Cooper commentedon Oct 5, 2014
In my models file? Could you please clarify? Because I only ever require Sequelize, I don't believe I have access to the pg that Sequelize uses unless I modify Sequelize code, which I can not do.
janmeier commentedon Oct 6, 2014
pg exports a singleton object, so
require('pg')in your code will return the same object that sequelize has access to. I just tested it, and doingrequire('pg').defaults.parseInt8 = trueoutside of sequelize code works fineRoam-Cooper commentedon Oct 10, 2014
Oh, that's awesome then! Thanks a tonne for your help, guys. :)
kikar commentedon Aug 30, 2017
Same thing is happening to me with Mysql/MariaDB, how to fix this?
kikar commentedon Aug 30, 2017
I've tried dialectOptions:
Following Sequelize docs and mysql docs, but still now working.
rodrigoclp commentedon Nov 30, 2018
Add ::integer
SELECT count(id)::integer AS "countName" FROM "TABLE_NAME"