-
I am about to port my existing code to work with the master branch and stumble about problems with use diesel::dsl::{Or, Eq};
use diesel::expression::bound::Bound;
use diesel::expression::{ Expression };
use diesel::sql_types::{ Bool, Text, Nullable };
type FiBound<FI> = Bound<<FI as Expression>::SqlType, &'static str>;
type BoolBound = Bound<Bool, bool>;
fn filter<FI>(field: FI) -> Or<BoolBound,Eq<FI, FiBound<FI>>>
where
FI: diesel::expression_methods::ExpressionMethods<SqlType = Nullable<Text>>
{
use diesel::expression::AsExpression;
use diesel::BoolExpressionMethods;
let fn_bool = |v| <bool as AsExpression<Bool>>::as_expression(v);
fn_bool(true).or(field.eq(""))
}
fn main() {
} Error on master is
Replacing the How can I express this with master? For reference: real code at |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
We changed our type tracking to be more in line with what database actually do. use diesel::dsl::{Or, Eq, AsExprOf};
use diesel::sql_types::{ Bool, Text, Nullable };
use diesel::{IntoSql, ExpressionMethods};
type FiBound<ST> = AsExprOf<&'static str, ST>;
type BoolBound = AsExprOf<bool, Bool>;
fn filter<FI>(field: FI) -> Or<BoolBound, Eq<FI, FiBound<FI::SqlType>>, Nullable<Bool>>
where
FI: ExpressionMethods<SqlType = Nullable<Text>>,
{
use diesel::BoolExpressionMethods;
true.into_sql::<Bool>().or(field.eq(""))
} (I also removed any usage of API's that are not part of our public API.) |
Beta Was this translation helpful? Give feedback.
We changed our type tracking to be more in line with what database actually do.
NULL OR some_expr
evaluates toNULL
, therefore we made sure that boolean expressions propagate nullability based on the arguments.In consequence that means as soon as at least on of your arguments may be nullable diesel requires that the output can be nullable. Due to type inference issues this can require a third type parameter to
diesel::dsl::Or
, as outlined in the documentation, to specify if that expression isNullable
or not.