Skip to content

Model.count returning NaN because Mysql returns an empty string #3019

Closed

Description

@cusspvz

Problem

Model.count is returning NaN because Mysql returns an empty string when grouping.

SELECT count(*) AS `count` FROM `Model` AS `Model` WHERE (`Model`.`deleted_at` IS NULL AND `Model`.`a_model`='Post' AND `Model`.`a_foreign_key`=186);
# count: 0
SELECT count(*) AS `count` FROM `Model` AS `Model` WHERE (`Model`.`deleted_at` IS NULL AND `Model`.`a_model`='Post' AND `Model`.`a_foreign_key`=186) GROUP BY `Model`.`user_id`;
# count: ''

If there are values to group up, everything works as expected, but if not, MySQL returns an empty string instead of a 0 (zero).

Solution

Return parsed Int, or 0 at queryInterface;

Activity

  1. added
    type: bug
    on Jan 29, 2015
  2. BridgeAR commented on May 17, 2015

    @BridgeAR
    Contributor

    @cusspvz Would you be so kind and check if your issue still persists and if, if my linked fix fixes the issue?

  3. janmeier commented on May 24, 2015

    @janmeier
    Member

    @cusspvz What result do you expect - a count for each group, or the number of groups?

    Asuming:

    user_id
    1
    1
    1
    2
    2

    Do you want

    [{ user_id: 1, count: 3 }, { user_id: 2, count: 2}]

    Or 2, the number of groups (which is guess is equal to COUNT DISTINCT user_id)

  4. cusspvz commented on May 26, 2015

    @cusspvz
    Author

    @BridgeAR

    @cusspvz Would you be so kind and check if your issue still persists and if, if my linked fix fixes the issue?

    Think your PR fixes this issue.

    @janmeier

    @cusspvz What result do you expect - a count for each group, or the number of groups?

    Honestly i din't quite remember, but after checking our internal issue related with this and the image it had, i think it is the number of users with entries with the polymorphed model.

    captura de ecra 2015-03-4 as 17 56 37

    Problem was on mysql returning an empty string instead of zero, you can check that behavior with mysql-client.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions