Cast enums in is_in and is_not_in (#1527) (#2002)

* Cast enums in `is_in` and `is_not_in` (#1527)

* Restore original tests, add tests for generated SQL
This commit is contained in:
Dmitrii Aleksandrov 2023-12-14 23:41:05 +04:00 committed by GitHub
parent 4b5fd9388e
commit 1abc47b5f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 2 deletions

View File

@ -62,7 +62,8 @@ macro_rules! bind_vec_func {
V: Into<Value>,
I: IntoIterator<Item = V>,
{
Expr::col((self.entity_name(), *self)).$func(v)
let v_with_enum_cast = v.into_iter().map(|v| self.save_as(Expr::val(v)));
Expr::col((self.entity_name(), *self)).$func(v_with_enum_cast)
}
};
}

View File

@ -8,7 +8,7 @@ use sea_orm::{
entity::prelude::*,
entity::*,
sea_query::{BinOper, Expr},
ActiveEnum as ActiveEnumTrait, DatabaseConnection,
ActiveEnum as ActiveEnumTrait, DatabaseConnection, QueryTrait,
};
#[sea_orm_macros::test]
@ -98,6 +98,7 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
.await?
.unwrap()
);
assert_eq!(
model,
Entity::find()
@ -109,6 +110,24 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
.await?
.unwrap()
);
// Equivalent to the above.
let select_with_tea_in = Entity::find().filter(Column::Tea.is_in([Tea::EverydayTea]));
assert_eq!(
select_with_tea_in
.build(sea_orm::DatabaseBackend::Postgres)
.to_string(),
[
"SELECT \"active_enum\".\"id\",",
"\"active_enum\".\"category\",",
"\"active_enum\".\"color\",",
"CAST(\"active_enum\".\"tea\" AS text)",
"FROM \"active_enum\"",
"WHERE \"active_enum\".\"tea\" IN (CAST('EverydayTea' AS tea))",
]
.join(" ")
);
assert_eq!(model, select_with_tea_in.one(db).await?.unwrap());
assert_eq!(
model,
Entity::find()
@ -121,6 +140,26 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
.await?
.unwrap()
);
// Equivalent to the above.
let select_with_tea_not_in = Entity::find()
.filter(Column::Tea.is_not_null())
.filter(Column::Tea.is_not_in([Tea::BreakfastTea]));
assert_eq!(
select_with_tea_not_in
.build(sea_orm::DatabaseBackend::Postgres)
.to_string(),
[
"SELECT \"active_enum\".\"id\",",
"\"active_enum\".\"category\",",
"\"active_enum\".\"color\",",
"CAST(\"active_enum\".\"tea\" AS text)",
"FROM \"active_enum\"",
"WHERE \"active_enum\".\"tea\" IS NOT NULL",
"AND \"active_enum\".\"tea\" NOT IN (CAST('BreakfastTea' AS tea))",
]
.join(" ")
);
assert_eq!(model, select_with_tea_not_in.one(db).await?.unwrap());
let res = model.delete(db).await?;

View File

@ -113,6 +113,15 @@ pub async fn insert_teas(db: &DatabaseConnection) -> Result<(), DbErr> {
.await?
.unwrap()
);
// Equivalent to the above.
assert_eq!(
model,
Entity::find()
.filter(Column::Id.is_in([Tea::EverydayTea]))
.one(db)
.await?
.unwrap()
);
let res = model.delete(db).await?;