-
Notifications
You must be signed in to change notification settings - Fork 76
Description
When sorting a sequelize query using query built attributes, we need to use sequelize.col('attribute_name') instead of a string, so sequelize won't prefix with the table name ("table_name"."attribute_name").
getOrder util method should look inside the model for the sorted attributes to choose between using string or sequelize.col method.
Steps to reproduce
Lets say you have users with a hasMany association to votes that you'd like to populate the count through a hook and let the client choose sorting parameters.
const association = {
attributes: [
'id',
'name',
[
sequelize.fn('COUNT', sequelize.col('votes.id')),
'count'
]
],
include: [
{
model: sequelize.models.votes,
attributes: [],
duplicating: false
}
],
group: ['users.id']
}Part of the generated query will contain the following selection: COUNT("votes"."id") AS count.
Client sorting by count would look like this:
service.find({
query: {
'$sort': {
count: -1
}
}
});The generated query will ORDER BY "users"."count" DESC which does not exist.
Expected behavior
Detect if sorting attributes are part of the model, using sequelize.col method when they are not.
Actual behavior
Every sort attribute is built to sequelize query as a string.