-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to order findAll() results by attribute on a join table #3173
Comments
We would likely look to extend the current |
How would one do this in the current version ? |
It's interesting that this hasn't been implemented, because actually I achieved this with another relationship query: With this example it would look like this :
This actually works, but if I try to get the projects from User model I can't order them. |
But would that not order by a column called |
Nope my example would order by a column called |
Ah, I see. Might have to go back and update some code to sort like that. As at the moment I have some raw sql in my code! (shudder) |
For now I would suggest : Sorting the javascript object after the query. Makes more sense, since it's far more maintainable. I'm putting this issue as reference though. Anyway |
using |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂 |
In my case the BelongsToMany isn't working as the join table has a createdAt and updatedAt field that I need to have reference to. The The idea is that orders and order changes have a many to many relation and the join table stores the created at field by date and time, and I need its latest order at all times. Not able to do that as of now, will try mapping it like this: orders => hasMany => join model => hasMany => order changes Will report updates here later on. Cheers! |
This also works for table aliases, but you have to use the table alias in the literal: UserProject.belongsTo(Project, {
foreignKey: "user_project_id",
as: "Project"
});
Sequelize.literal("Project.UserProjects.name"); |
@drinchev sorting in JS is completely pointless if you need to do any kind of paging, which is probably why your comment got so many downvotes. Do you really not need to do any kind of paging in your apps? |
I ran into a weird issue that the generated sql didn't have quotes around the include: [{
model: Outer,
as: 'outer',
through: {
model: Inner,
as: 'inner',
},
required: false,
}],
order: [
[Sequelize.literal('"outer.inner.createdAt"'), 'asc'],
], I thought I should post here for anyone else getting weird errors |
I was having an issue using Sql Server, I was getting the error The multi-part identifier "winner.new_apv" could not be bound.
Using the Sequelize literal and the single quotes in the literal itself as erfanio says did the trick
|
@erfanio Thanks for posting! With my luck on Sequelize, it's only a matter of time before I need this. Talk about a dumb solution having to tell the ORM when to use quotes. |
|
I'm still a little confused on this. Was this added as a feature? I have a table "events" and a table "users", with a join table "events-users". I assumed there would be a field for that somewhere in the belongsToMany definition here for example: |
I would like to request that the API would allow me to order by an attribute in a custom join table.
i.e.
Using the example in the documentation:
If table UserProject had an attribute or priority, I would like to be able to get results in a manner similar to this:
The text was updated successfully, but these errors were encountered: