@@ -21,6 +21,8 @@ pub struct ParsedField {
2121 pub sql_type : Option < String > ,
2222 /// Whether the SQL type is an array (needs `[]` suffix in cast)
2323 pub is_sql_array : bool ,
24+ /// Raw SQL default expression from the DB (e.g. "now()", "'idle'::task_status")
25+ pub column_default : Option < String > ,
2426}
2527
2628/// Represents an entity parsed from a generated Rust file.
@@ -189,7 +191,7 @@ fn extract_field(field: &syn::Field) -> Result<ParsedField, String> {
189191 . to_string ( ) ;
190192
191193 let column_name = get_sqlx_rename ( & field. attrs ) . unwrap_or_else ( || rust_name. clone ( ) ) ;
192- let ( is_primary_key, sql_type, is_sql_array) = parse_sqlx_gen_field_attrs ( & field. attrs ) ;
194+ let ( is_primary_key, sql_type, is_sql_array, column_default ) = parse_sqlx_gen_field_attrs ( & field. attrs ) ;
193195
194196 let rust_type = field. ty . to_token_stream ( ) . to_string ( ) ;
195197 let ( is_nullable, inner_type) = extract_option_type ( & field. ty ) ;
@@ -208,15 +210,17 @@ fn extract_field(field: &syn::Field) -> Result<ParsedField, String> {
208210 is_primary_key,
209211 sql_type,
210212 is_sql_array,
213+ column_default,
211214 } )
212215}
213216
214217/// Parse `#[sqlx_gen(...)]` attributes on a field.
215- /// Returns (is_primary_key, sql_type, is_sql_array).
216- fn parse_sqlx_gen_field_attrs ( attrs : & [ syn:: Attribute ] ) -> ( bool , Option < String > , bool ) {
218+ /// Returns (is_primary_key, sql_type, is_sql_array, column_default ).
219+ fn parse_sqlx_gen_field_attrs ( attrs : & [ syn:: Attribute ] ) -> ( bool , Option < String > , bool , Option < String > ) {
217220 let mut is_pk = false ;
218221 let mut sql_type = None ;
219222 let mut is_array = false ;
223+ let mut column_default = None ;
220224
221225 for attr in attrs {
222226 if attr. path ( ) . is_ident ( "sqlx_gen" ) {
@@ -230,10 +234,13 @@ fn parse_sqlx_gen_field_attrs(attrs: &[syn::Attribute]) -> (bool, Option<String>
230234 if tokens. contains ( "is_array" ) {
231235 is_array = true ;
232236 }
237+ if let Some ( d) = extract_attr_value ( & tokens, "column_default" ) {
238+ column_default = Some ( d) ;
239+ }
233240 }
234241 }
235242
236- ( is_pk, sql_type, is_array)
243+ ( is_pk, sql_type, is_array, column_default )
237244}
238245
239246/// Extract `#[sqlx(rename = "...")]` value from field attributes.
@@ -607,4 +614,56 @@ mod tests {
607614 assert_eq ! ( entity. imports. len( ) , 1 ) ;
608615 assert ! ( entity. imports[ 0 ] . contains( "chrono" ) ) ;
609616 }
617+
618+ // --- column_default parsing ---
619+
620+ #[ test]
621+ fn test_parse_column_default ( ) {
622+ let source = r#"
623+ #[derive(Debug, Clone, sqlx::FromRow)]
624+ #[sqlx_gen(kind = "table", table = "tasks")]
625+ pub struct Tasks {
626+ #[sqlx_gen(primary_key)]
627+ pub id: i32,
628+ #[sqlx_gen(column_default = "now()")]
629+ pub created_at: String,
630+ }
631+ "# ;
632+ let entity = parse_entity_source ( source) . unwrap ( ) ;
633+ let created_at = & entity. fields [ 1 ] ;
634+ assert_eq ! ( created_at. column_default, Some ( "now()" . to_string( ) ) ) ;
635+ }
636+
637+ #[ test]
638+ fn test_parse_no_column_default ( ) {
639+ let source = r#"
640+ #[derive(Debug, Clone, sqlx::FromRow)]
641+ #[sqlx_gen(kind = "table", table = "tasks")]
642+ pub struct Tasks {
643+ #[sqlx_gen(primary_key)]
644+ pub id: i32,
645+ pub title: String,
646+ }
647+ "# ;
648+ let entity = parse_entity_source ( source) . unwrap ( ) ;
649+ let title = & entity. fields [ 1 ] ;
650+ assert_eq ! ( title. column_default, None ) ;
651+ }
652+
653+ #[ test]
654+ fn test_parse_column_default_with_cast ( ) {
655+ let source = r#"
656+ #[derive(Debug, Clone, sqlx::FromRow)]
657+ #[sqlx_gen(kind = "table", table = "tasks")]
658+ pub struct Tasks {
659+ #[sqlx_gen(primary_key)]
660+ pub id: i32,
661+ #[sqlx_gen(column_default = "'idle'::task_status")]
662+ pub status: String,
663+ }
664+ "# ;
665+ let entity = parse_entity_source ( source) . unwrap ( ) ;
666+ let status = & entity. fields [ 1 ] ;
667+ assert_eq ! ( status. column_default, Some ( "'idle'::task_status" . to_string( ) ) ) ;
668+ }
610669}
0 commit comments