Skip to content

Count with associations #11162

Closed
Closed

Description

@kaikash

What are you doing?

To Reproduce
Steps to reproduce the behavior:
Let's imagine we have two models User and Image. And Image has two fields:

imageableType: {
  type: DataTypes.STRING,
  field: 'imageable_type'
},
imageableId: {
  type: DataTypes.INTEGER,
  field: 'imageable_id'
}

Somewhere in User model:

this.images = User.hasMany(Image, {
  foreignKey: 'imageableId',
  as: 'images',
  scope: {
    imageableType: 'user'
  }
};

We try to call count method

const count = await User.count({
  include: [{
    association: User.images
  }]
});

Update: I didn't mention I have underscored: true option in models

What do you expect to happen?

We want to get a number!

What is actually happening?

But we get error: column images.imageableType does not exist

SQL: SELECT count("User"."id") AS "count" FROM "users" AS "User" LEFT OUTER JOIN "images" AS "images" ON "User"."id" = "images"."imageable_id" AND ("images"."imageableType" = 'user');
Actually, there should be "images"."imageable_type" = 'user'

And we get same error if we call findAndCountAll

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any
    Dialect library version: XXX
    Database version: 11.2
    Sequelize version: 5.6.1
    Node Version: 11.7.0
    OS: macOS Mojave
    Tested with latest release:
  • No
  • Yes, specify that version:

Activity

  1. mamogaaa commented on Jul 11, 2019

    @mamogaaa

    Same problem

  2. kaikash commented on Jul 22, 2019

    @kaikash
    Author
  3. papb commented on Jul 22, 2019

    @papb
    Member

    Could you please test with another dialect, such as SQLite?

    You can connect to an in-memory sqlite db by using new Sequelize('sqlite::memory:').

  4. added
    status: awaiting response
    type: bug
    and removed on Jul 22, 2019
  5. kaikash commented on Jul 25, 2019

    @kaikash
    Author

    @papb I'm on vacation now, so I can't check it now.

    I didn't mention I have underscored: true option in models

  6. papb commented on Jul 25, 2019

    @papb
    Member

    @kaikash No problem, take your time! @nordikafiles Can you help?

  7. kaikash commented on Jul 25, 2019

    @kaikash
    Author

    @papb when using SQLite it makes query like this:

    SELECT count(*) AS `count` FROM `users` AS `User` LEFT OUTER JOIN `images` AS `image` ON `User`.`id` = `image`.`imageable_id` AND (`image`.`deleted_at` IS NULL AND `image`.`imageableType` = 'user') WHERE (`User`.`deleted_at` IS NULL);

    As you can see there is still imageableType instead of imageable_type

  8. papb commented on Jul 25, 2019

    @papb
    Member

    Closing in favor of #11225.

    When #11225 gets closed, if anyone thinks this particular issue is still an issue, please ping me.

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

    status: awaiting responsetype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions