Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upIs there a convenient way to construct a query in raw SQL? #231
Comments
This comment has been minimized.
|
Yes, there's a function for writing raw SQL (though its use is discouraged as you lose the vast majority of the safety benefits the query builder provides). http://sgrif.github.io/diesel/diesel/expression/dsl/fn.sql.html This query written using the query builder is basically one-to-one with the equivalent SQL. use diesel::prelude::*;
use diesel::expression::{sql, count};
use hosts::dsl::*;
hosts.select(id, hostname, created_at, updated_at)
.left_outer_join(logs::table)
.filter(logs::app_id.eq(whatever))
.group_by((hostname, id))
.order(count(logs::app_id).asc())
.limit(1) |
sgrif
closed this
Mar 4, 2016
This comment has been minimized.
|
Follow up question: is it possible to have multiple joins? I see a test for a join through, but what about cases where there isn't a through? What I want: logs::table.select((logs::id, users::id, hosts::id))
.inner_join(hosts::table)
.inner_join(users::table)
.limit(1) |
This comment has been minimized.
|
No, this is a major limitation at the moment. We need specialization to do On Fri, Mar 18, 2016, 11:22 AM Andrew Ryan Lazarus notifications@github.com
|
nerdrew commentedMar 4, 2016
Example query I'd like to run:
I'd like to be able to select the query above into a
Hostmodel.Follow up question: is there a way to represent the above query in diesel? I couldn't figure it out.
The subject stands though, even if/when there is a way to represent the query in diesel's DSL. There are times when it is clearer to write sql and it would be nice if diesel had a convenient story for supporting that use case.