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

How to build a conditional query #455

Closed
TheNeikos opened this Issue Oct 2, 2016 · 6 comments

Comments

Projects
None yet
5 participants
@TheNeikos

TheNeikos commented Oct 2, 2016

Due to the fact that the filter method returns a new type I'm unable to create a 'dynamic' query. Basically I want to do something like this:

let mut query = submissions.limit(self.settings.limit));

if let Some(user) = self.submitter {
    query = query.filter(user_id.eq(user.id));
}

if let Some(n) = self.title {
    query = query.filter(title.like(n));
}

if let Some(desc) = self.description {
    query = query.filter(description.like(desc));
}

This fails to compile due to the fact that query gets typed right away and it might or might not change.
Is this something that can be addressed? I'd rather not have N! code paths to run through.
A macro would be a suitable replacement, however I am not knowledgeable enough to create it.

@killercup

This comment has been minimized.

Member

killercup commented Oct 2, 2016

One way to solve this is to box the query in each branch (with
.into_boxed()).

Marcel Müller notifications@github.com schrieb am So. 2. Okt. 2016 um
05:24:

Due to the fact that the filter method returns a new type I'm unable to
create a 'dynamic' query. Basically I want to do something like this:

let mut query = submissions.limit(self.settings.limit));

if let Some(user) = self.submitter {
query = query.filter(user_id.eq(user.id));
}

if let Some(n) = self.title {
query = query.filter(title.like(n));
}

if let Some(desc) = self.description {
query = query.filter(description.like(desc));
}

This fails to compile due to the fact that query gets typed right away and
it might or might not change.
Is this something that can be addressed? I'd rather not have N! code
paths to run through.
A macro would be a suitable replacement, however I am not knowledgeable
enough to create it.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#455, or mute the thread
https://github.com/notifications/unsubscribe-auth/AABOXy9p9ZxezbegJVxvvvQt3ox69NM5ks5qvyPqgaJpZM4KL7Dn
.

@TheNeikos

This comment has been minimized.

TheNeikos commented Oct 2, 2016

Hm, doing that gives me this error:

error[E0275]: overflow evaluating the requirement `<diesel::query_builder::BoxedSelectStatement<'_, (diesel::types::BigInt, diesel::types::BigInt, diesel::types::Timestamp, diesel::types::Timestamp, diesel::types::BigInt, diesel::types::Text, diesel::types::Text, diesel::types::Nullable<diesel::types::Timestamp>, diesel::types::Integer), models::schema::submissions::table, _> as diesel::boxed_dsl::InternalBoxedDsl<'_, _>>::Output`
   --> src/models/submission.rs:389:55
    |
389 |             query = query.filter(user_id.eq(user.id)).into_boxed();
    |                                                       ^^^^^^^^^^
    |
    = note: consider adding a `#![recursion_limit="128"]` attribute to your crate

Adding that attribute just raises it to 256 :v


Edit: Okay, adding into_boxed to the first line makes it compile. So there is that.

Thanks!

@TheNeikos TheNeikos closed this Oct 2, 2016

@sgrif

This comment has been minimized.

Member

sgrif commented Oct 3, 2016

Yeah, you're correct. You'll need to box it before the branch, but you won't need to box it again in each branch. Once it's boxed it stays boxed forever.

@pka

This comment has been minimized.

pka commented Jan 27, 2018

After an hour or two of trying to compose a query like this, I gave up and looked through all issues finding this solution on page 15 of the closed tickets

This deserves a hint in the documentation (or a more prominent one, in case it's already documented). The ability to compose queries programmatically in a readable way is a fundamental advantage of an ORM compared to raw SQL. Thanks for this great library!

@weiznich

This comment has been minimized.

Contributor

weiznich commented Jan 27, 2018

This deserves a hint in the documentation (or a more prominent one, in case it's already documented)

It is already in the docs. For example here.
Do you have any suggestion where we may add a more prominent hint?

@pka

This comment has been minimized.

pka commented Jan 27, 2018

What about a sentence in the Getting Started guide like "In cases where you want to conditionally modify a query, use into_boxed to box the pieces of a query into a single type."?

@barzamin barzamin referenced this issue Jun 2, 2018

Merged

Bios: linkify entities #49

5 of 5 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment