Skip to content

Commit 943bfb1

Browse files
enkhjilecursoragent
authored andcommitted
Fix entity codegen for PostgreSQL enum array columns
Array enum columns were emitted as scalar active-enum types because get_column_rs_types_with_enum_idents used get_inner_col_type(), which unwraps Array(Enum) to Enum. Route array columns through get_rs_type() instead. Fixes #3120 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent f947586 commit 943bfb1

1 file changed

Lines changed: 59 additions & 1 deletion

File tree

sea-orm-codegen/src/entity/writer.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,9 @@ impl EntityWriter {
687687
.columns
688688
.iter()
689689
.map(|col| {
690-
if let sea_query::ColumnType::Enum { name, .. } = col.get_inner_col_type()
690+
if matches!(&col.col_type, sea_query::ColumnType::Array(_)) {
691+
col.get_rs_type(column_option)
692+
} else if let sea_query::ColumnType::Enum { name, .. } = col.get_inner_col_type()
691693
&& let Some(enum_type_ident) = active_enum_type_idents.get(&name.to_string())
692694
{
693695
if col.not_null {
@@ -1894,6 +1896,62 @@ mod tests {
18941896
}
18951897
}
18961898

1899+
#[test]
1900+
fn test_enum_array_column_generates_vec() {
1901+
let enum_ty = ColumnType::Enum {
1902+
name: SeaRc::new(Alias::new("subscription_status")),
1903+
variants: vec![SeaRc::new(Alias::new("ACTIVE"))],
1904+
};
1905+
let entity = Entity {
1906+
table_name: "subscriber".to_owned(),
1907+
columns: vec![
1908+
Column {
1909+
name: "statuses".to_owned(),
1910+
col_type: ColumnType::Array(RcOrArc::new(enum_ty.clone())),
1911+
auto_increment: false,
1912+
not_null: true,
1913+
unique: false,
1914+
unique_key: None,
1915+
},
1916+
Column {
1917+
name: "status".to_owned(),
1918+
col_type: enum_ty,
1919+
auto_increment: false,
1920+
not_null: true,
1921+
unique: false,
1922+
unique_key: None,
1923+
},
1924+
],
1925+
relations: vec![],
1926+
conjunct_relations: vec![],
1927+
primary_keys: vec![],
1928+
};
1929+
let body = EntityWriter::gen_dense_code_blocks(
1930+
&entity,
1931+
&WithSerde::None,
1932+
&default_column_option(),
1933+
&None,
1934+
false,
1935+
false,
1936+
&TokenStream::new(),
1937+
&TokenStream::new(),
1938+
&TokenStream::new(),
1939+
false,
1940+
true,
1941+
)
1942+
.into_iter()
1943+
.skip(1)
1944+
.fold(TokenStream::new(), |mut acc, tok| {
1945+
acc.extend(tok);
1946+
acc
1947+
})
1948+
.to_string();
1949+
1950+
assert!(body.contains("statuses : Vec < SubscriptionStatus >"));
1951+
assert!(body.contains("status : SubscriptionStatus"));
1952+
assert!(!body.contains("statuses : SubscriptionStatus"));
1953+
}
1954+
18971955
#[test]
18981956
fn test_gen_expanded_code_blocks() -> io::Result<()> {
18991957
let entities = setup();

0 commit comments

Comments
 (0)