-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Variadic groupBy / orderBy #30
Comments
I've added the try conn.select()
.column(.count(\Planet.name))
.from(Planet.self)
.where(\Planet.galaxyID, .equal, 5)
.run().wait()
try conn.select()
.column(.count(.all))
.from(Planet.self)
.where(\Planet.galaxyID, .equal, 5)
.run().wait()
try conn.select()
.column(.sum(\Planet.id), as: "id_sum")
.from(Planet.self)
.where(\Planet.galaxyID, .equal, 5)
.run().wait() I think this is a bit more future proof than adding directly to the I'll leave this issue open to discuss the best way of adding |
The variadics are just a convenience. I usually am doing more than one SUM, for example, so it just makes the code a bit cleaner. The group by one, though, automatically adds the item being grouped to the select list. If you're grouping by it, you MUST select it, so why make them explicitly add it? |
Hey @tanner0101, any update on the groupBy option? |
@grosch I'm not sure the signature as exists would work: func groupBy<T,V>(_ keyPaths: KeyPath<T, V>...) This only allows you to add key paths of the same model and value. For example, what if you wanted to do something like: try conn.select()
.column(.star)
.from(Planet.self)
.join(Galaxy.self, on: \Planet.galaxyID == \Galaxy.id)
.groupBy(\Planet.name, \Galaxy.name)
.run().wait() This wouldn't work because the root model type is not equal (Galaxy != Planet). The same thing also prevents grouping by two different columns on the same model if the column type is different. |
@tanner0101 The sql you showed isn't valid though. If you GROUP BY something, those items all have to be in the select list. |
@tanner0101 , after out long chat on Discord, I ended up with these convenience methods. Like you said, they only work if all arguments are the same for the groupBy and orderBy, but that you knew how to fix. Hopefully this can turn into a real thing
The text was updated successfully, but these errors were encountered: