Skip to content

(Aliased) Count in raw query returns string instead of integer for the count column #2383

Closed

Description

@Roam-Cooper

When not binding the results to an orm model and therefore using raw: true,

A SELECT count(id) AS "countName" FROM "TABLE_NAME";
Results in the count being returned as a string, rather than an integer in Sequelize 1.7.9

Activity

  1. Roam-Cooper commented on Oct 2, 2014

    @Roam-Cooper
    Author

    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.

  2. mickhansen commented on Oct 3, 2014

    @mickhansen
    Contributor

    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.

  3. Roam-Cooper commented on Oct 4, 2014

    @Roam-Cooper
    Author

    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

  4. mickhansen commented on Oct 4, 2014

    @mickhansen
    Contributor

    @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.

  5. Roam-Cooper commented on Oct 5, 2014

    @Roam-Cooper
    Author

    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.

  6. janmeier commented on Oct 5, 2014

    @janmeier
    Member

    In your own models file you should be able to require pg and do the changes mentioned in the issue you linked

  7. Roam-Cooper commented on Oct 5, 2014

    @Roam-Cooper
    Author

    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.

  8. janmeier commented on Oct 6, 2014

    @janmeier
    Member

    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

  9. Roam-Cooper commented on Oct 10, 2014

    @Roam-Cooper
    Author

    Oh, that's awesome then! Thanks a tonne for your help, guys. :)

  10. kikar commented on Aug 30, 2017

    @kikar

    Same thing is happening to me with Mysql/MariaDB, how to fix this?

  11. kikar commented on Aug 30, 2017

    @kikar

    I've tried dialectOptions:

    new Sequelize('mysql://root@localhost:3306/database', { dialectOptions: { supportBigNumbers: true }})

    Following Sequelize docs and mysql docs, but still now working.

  12. rodrigoclp commented on Nov 30, 2018

    @rodrigoclp

    Add ::integer
    SELECT count(id)::integer AS "countName" FROM "TABLE_NAME"

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions