Hello,
I'm migrating an sqlx project to Diesel/Diesel-async, because Sqlx doesn't handle Oracle databases.
but I've hit a problem with Raw sql queries
To just do a count(*), we need to create a type, for example, using type i64 does not work :
diesel::sql_query(r#"SELECT COUNT(*) FROM table_xxxx"#)
.get_result::<i64>(conn);
Error message :
.get_result::<i64>(conn)
| ^^^^^^^^^^ the trait `CompatibleType<i64, Pg>` is not implemented for `Untyped`, which is required by `SqlQuery: diesel_async::methods::LoadQuery<'_, _, i64>`
|
= help: the trait `CompatibleType<U, DB>` is implemented for `Untyped`
= note: required for `SqlQuery` to implement `diesel_async::methods::LoadQuery<'_, _, i64>`
to resolve this problem, we need to create a type to represent the count(*) :
#[derive(QueryableByName)]
pub struct CountType {
#[diesel(sql_type = diesel::sql_types::BigInt)]
pub count: i64,
}
diesel::sql_query(r#"SELECT COUNT(*) FROM table_xxxx"#)
.get_result::<CountType>(conn); // <CountType> instead of <i64>
but if we want to select other fields? aren't we going to create several types for each combination?
for example, if we use this query ( dummy query, just to show the idea ) :
SELECT COUNT(*) , 888 as column_b, column_b FROM table_xxxx
we need to create another struct composed of ( count, column_a ,column_b) ?!
I really hope this isn't the case, otherwise I'll have wasted a week migrating for nothing.
if anyone knows more about Diesel, please tell me about a simpler solution without having to create Structs for each query.