Question

Is there a way to select distinct rows from a table using sequelize.js? I looked through the documentation but the "finder methods" do not specify a way to accomplish this task.

Was it helpful?

Solution

It's not possible automatically but if you don't mind creating the sql on your own, you could do this:

sequelize.query('sql goes here', null, { raw: plain }).success(function(data){
    console.log(data)
})

Have fun :)

UPDATE

  1. Sequelize now uses then instead of success as promise function.
  2. Sequelize.query has been refactored to only use paramters sql and options
  3. raw accepts true/false but no plain as value.

So, according to the new version, the code should look like this:

    sequelize.query('sql goes here', { raw: true }).then(function(data){
        console.log(data);
    });

OTHER TIPS

Assuming you want to apply DISTINCT to the following query:

Tuple.findAll({attributes: ['key', 'value']});

then this is a (hackish) way to achieve what you want without having to write the whole query yourself:

Tuple.findAll({attributes: [[Sequelize.literal('DISTINCT `key`'), 'key'], 'value']});

(Tested with Sequelize v2.1.0)

Edit 2015-06-08: Still works with Sequelize v3.1.1

You can do the following:

myModel.findAll({
  attributes: [[sequelize.fn('DISTINCT', sequelize.col('col_name')), 'alias_name']],
  where:{}
}).then(data => {}).....

taken from issues and it works.

edit your "node_modules/sequelize/lib/dialects/mysql/query-generator.js"

at around line 118

change

var query = "SELECT <%= attributes %> FROM <%= table %>"

into

var query = "SELECT " + ((options.distinct)? 'DISTINCT ':'') +"<%= attributes %> FROM <%= table %>",

now you can add an option distinct: true in your sequelize request

hope it helps -_^

Model.findAll({Attributes: ['col_name1', 'col_name2'], group: ['col_name1', 'col_name2']});

it's perfectly fine with Sequelize 5.21

This is somewhat similar to the solution proposed by Pascal Ludwig, but for those landing here looking to get a list of distinct values for a given column, you can do the following:

MyModel.aggregate('teh_field', 'DISTINCT', { plain: false }).then(...)
// Resolves to: [ { DISTINCT: value1 }, { DISTINCT: value2 }, ... ]

With that, it's easy to transform it into a standard list:

MyModel.aggregate('teh_field', 'DISTINCT', { plain: false })
    .map(function (row) { return row.DISTINCT })
    .then(function (tehValueList) {
        // tehValueList = [ value1, value2, ... ]
    })
;

As of Sequelize version 1.7, the select query has been moved into lib/dialects/abstract/query-generator.js.

Around line 1167, change

mainQueryItems.push("SELECT "+mainAttributes.join ....)

to

    mainQueryItems.push('SELECT ');
    if (options.distinct) {
        mainQueryItems.push('DISTINCT ');
    }
    mainQueryItems.push(mainAttributes.join(', ') + ' FROM ' + options.table);

By the way, I use Sqlite and Postgres, both of which support "DISTINCT". If you're using a dialect that doesn't support distinct, then obviously this line will cause problems for you, since it will be generated for all the SQL flavors that you're using. I suspect this is why this simple change hasn't made it into the main Sequelize source tree.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top