Skip to content
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

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

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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)
}
};
}
Expand Down
41 changes: 40 additions & 1 deletion tests/active_enum_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -98,6 +98,7 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
.await?
.unwrap()
);

assert_eq!(
model,
Entity::find()
Expand All @@ -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()
Expand All @@ -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?;

Expand Down
9 changes: 9 additions & 0 deletions tests/enum_primary_key_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
Loading