Skip to content

Can you change the order of "ORDER BY" columns created from "nested" associations in a request? #1497

Answered by groue
SwiftedMind asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @SwiftedMind,

To change the order, I would need to be able to reference all the needed columns within the same .order() call but that's not possible because I don't know how to reference a "nested" column on a JOINed table.

Your intuition is spot on. This is exactly how it should be done 👍

In the (too long) documentation for associations, the relevant section is Table Aliases.

Practically speaking, you can adapt your code as below. I moved the ordering up to the outer request of items, so I needed two aliases, one for presentations and one for tags:

let presentationAlias = TableAlias()
let tagAlias = TableAlias()
let presententation = Item.presentation.aliased(presentationAlias)
let 

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@SwiftedMind
Comment options

@groue
Comment options

Answer selected by SwiftedMind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants