Reputation: 160
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.
Upvotes: 14
Views: 62176
Reputation: 1024
Model.findAll({Attributes: ['col_name1', 'col_name2'], group: ['col_name1', 'col_name2']});
it's perfectly fine with Sequelize 5.21
Upvotes: 5
Reputation: 3855
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.
Upvotes: 18
Reputation: 6241
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
then
instead of success
as promise function.Sequelize.query
has been refactored to only use paramters sql
and options
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);
});
Upvotes: 3
Reputation: 6124
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, ... ]
})
;
Upvotes: 2
Reputation: 1095
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
Upvotes: 21
Reputation: 1603
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.
Upvotes: 0
Reputation: 91
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 -_^
Upvotes: 5