+
Skip to content
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
26 changes: 14 additions & 12 deletions sea-orm-macros/src/derives/partial_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,19 @@ impl DerivePartialModel {

// variant of the entity column
let column = quote! {
<#entity as ::sea_orm::EntityTrait>::Column::#variant_name
<#entity as sea_orm::EntityTrait>::Column::#variant_name
};

let maybe_aliased_column = match alias {
// We cast enum as text in select_as if the backend is postgres

let non_nested = match alias {
Some(alias) => quote! {
::sea_orm::sea_query::Expr::col((#alias, #column))
let col_expr = sea_orm::sea_query::Expr::col((#alias, #column));
let casted = sea_orm::ColumnTrait::select_as(&#column, col_expr);
sea_orm::SelectColumns::select_column_as(#select_ident, casted, col_alias)
},
None => quote! {
#column
sea_orm::SelectColumns::select_column_as(#select_ident, #column, col_alias)
},
};

Expand All @@ -288,18 +292,16 @@ impl DerivePartialModel {
let col_alias = pre.map_or(#field.to_string(), |pre| format!("{pre}{}", #field));
if let Some(nested_alias) = nested_alias {
// TODO: Replace this with the new iden after updating to sea-query 1.0
let alias = ::sea_orm::sea_query::Alias::new(nested_alias);
let alias_iden = ::sea_orm::sea_query::DynIden::new(alias);
let col_expr = ::sea_orm::sea_query::Expr::col(
let alias = sea_orm::sea_query::Alias::new(nested_alias);
let alias_iden = sea_orm::sea_query::DynIden::new(alias);
let col_expr = sea_orm::sea_query::Expr::col(
(alias_iden, #column)
);

// Cast enum as text if the backend is postgres
let col_expr = ::sea_orm::ColumnTrait::select_as(&#column, col_expr);

::sea_orm::SelectColumns::select_column_as(#select_ident, col_expr, col_alias)
let casted = sea_orm::ColumnTrait::select_as(&#column, col_expr);
sea_orm::SelectColumns::select_column_as(#select_ident, casted, col_alias)
} else {
::sea_orm::SelectColumns::select_column_as(#select_ident, #maybe_aliased_column, col_alias)
#non_nested
}
};
}
Expand Down
36 changes: 35 additions & 1 deletion tests/active_enum_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,6 @@ mod tests {
tea: Option<Tea>,
}

// Build a query that would be produced by into_partial_model()
let sql = active_enum::Entity::find()
.into_partial_model::<PartialWithEnum>()
.into_statement(DbBackend::Postgres)
Expand All @@ -1077,6 +1076,23 @@ mod tests {
r#"SELECT CAST("active_enum"."tea" AS "text") AS "tea" FROM "public"."active_enum""#,
);

#[derive(Debug, DerivePartialModel)]
#[sea_orm(entity = "active_enum::Entity", from_query_result, alias = "zzz")]
struct PartialWithEnumAndAlias {
#[sea_orm(from_col = "tea")]
foo: Option<Tea>,
}

let sql = active_enum::Entity::find()
.into_partial_model::<PartialWithEnumAndAlias>()
.into_statement(DbBackend::Postgres)
.sql;

assert_eq!(
sql,
r#"SELECT CAST("zzz"."tea" AS "text") AS "foo" FROM "public"."active_enum""#,
);

#[derive(Debug, DerivePartialModel)]
#[sea_orm(entity = "active_enum::Entity", from_query_result)]
struct PartialWithEnumNested {
Expand All @@ -1094,5 +1110,23 @@ mod tests {
sql,
r#"SELECT CAST("active_enum"."tea" AS "text") AS "tea", CAST("foo"."tea" AS "text") AS "nested_tea" FROM "public"."active_enum""#,
);

#[derive(Debug, DerivePartialModel)]
#[sea_orm(entity = "active_enum::Entity", from_query_result, alias = "aaa")]
struct PartialWithEnumNestedWithAlias {
tea: Option<Tea>,
#[sea_orm(nested, alias = "foo")]
nested: Option<PartialWithEnum>,
}

let sql = active_enum::Entity::find()
.into_partial_model::<PartialWithEnumNestedWithAlias>()
.into_statement(DbBackend::Postgres)
.sql;

assert_eq!(
sql,
r#"SELECT CAST("aaa"."tea" AS "text") AS "tea", CAST("foo"."tea" AS "text") AS "nested_tea" FROM "public"."active_enum""#,
);
}
}
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载