How do you supply a vector of values to a custom query for use in an in
operator?`
#497
Replies: 8 comments 1 reply
-
Hey @robot-head, you can use the |
Beta Was this translation helpful? Give feedback.
-
Right, but I want to do this in a custom query instead. I've simplified the query for the bug/example, but it's actually a quite complex query with subselects etc that I do not wish to express using the ORM. |
Beta Was this translation helpful? Give feedback.
-
I think you need to write raw sql completely, i.e. write the values into sql directly instead of binding it |
Beta Was this translation helpful? Give feedback.
-
That's what I was afraid of, and wanted to avoid doing so I was protected from sql injection etc. It looks like sea-query supports this in the Could we do similar for sea-orm? https://github.com/SeaQL/sea-query/blob/master/src/value.rs#L76-L78 |
Beta Was this translation helpful? Give feedback.
-
As a temporary solution, you can do SELECT column
FROM table
WHERE column IN ($1, $2, $3) let possible_values = vec![1i32, 2i32, 3i32];
let model = Model::find_by_statement(Statement::from_sql_and_values(
DatabaseBackend::Postgres,
include_str!("query.sql"),
possible_values.into_iter().map(Into::into),
)) |
Beta Was this translation helpful? Give feedback.
-
Well, unfortunately the size of the array is variable so that won't work. |
Beta Was this translation helpful? Give feedback.
-
I wonder how complex is your query in |
Beta Was this translation helpful? Give feedback.
-
I think your best bet is to build the sub-query portion of the SQL using sea-query and inject that into your bigger SQL (using a |
Beta Was this translation helpful? Give feedback.
-
I'm trying to do a
find_by_statement
on a model, but my query contains something like:`select column from table where column in (1,2,3)'
If I try to implement it as such, it does not compile:
query.sql
:Alternatively, if I try it as such:
I get a query error as the possible values become single quoted and interpreted as a string.
Beta Was this translation helpful? Give feedback.
All reactions