How to get a distinct count with sequelize?

sequelize.js

Solution

Looks like this is now supported in Sequelize versions 1.7.0+.

the `count` and `findAndCountAll` methods of a model will give you 'real' or 'distinct' count of your parent model.

Problem

I am trying to get a distinct count of a particular column using sequelize. My initial attempt is using the 'count' method of my model, however it doesn't look like this is possible. The DISTINCT feature is needed because I am joining other tables and filtering the rows of the parent based on the related tables. here's the query I would like: ``` SELECT COUNT(DISTINCT Product.id) as `count` FROM `Product` LEFT OUTER JOIN `Vendor` AS `vendor` ON `vendor`.`id` = `Product`.`vendorId` WHERE (`vendor`.`isEnabled`=true ); ``` using the following query against my Product model: ``` Product.count({ include: [{model: models.Vendor, as: 'vendor'}], where: [{ 'vendor.isEnabled' : true }] }) ``` Generates the following query: ``` SELECT COUNT(*) as `count` FROM `Product` LEFT OUTER JOIN `Vendor` AS `vendor` ON `vendor`.`id` = `Product`.`vendorId` WHERE (`vendor`.`isEnabled`=true ); ```

Original source

Related problems