Skip to content

Commit

Permalink
Fix: fields with type Option<T> are always nullable (#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored and tyt2y3 committed Dec 2, 2022
1 parent 8116d2d commit cdbb8c7
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,21 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
});
}

let field_type = &field.ty;
let field_type = quote! { #field_type }
.to_string() //E.g.: "Option < String >"
.replace(' ', ""); // Remove spaces
let field_type = if field_type.starts_with("Option<") {
nullable = true;
&field_type[7..(field_type.len() - 1)] // Extract `T` out of `Option<T>`
} else {
field_type.as_str()
};

let col_type = match sql_type {
Some(t) => quote! { sea_orm::prelude::ColumnType::#t.def() },
None => {
let field_type = &field.ty;
let temp = quote! { #field_type }
.to_string() //E.g.: "Option < String >"
.replace(' ', "");
let temp = if temp.starts_with("Option<") {
nullable = true;
&temp[7..(temp.len() - 1)]
} else {
temp.as_str()
};
let col_type = match temp {
let col_type = match field_type {
"char" => quote! { Char(None) },
"String" | "&str" => quote! { String(None) },
"i8" => quote! { TinyInteger },
Expand Down Expand Up @@ -270,7 +271,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
};
if col_type.is_empty() {
let field_span = field.span();
let ty: Type = LitStr::new(temp, field_span).parse()?;
let ty: Type = LitStr::new(field_type, field_span).parse()?;
let def = quote_spanned! { field_span => {
std::convert::Into::<sea_orm::ColumnType>::into(
<#ty as sea_orm::sea_query::ValueType>::column_type()
Expand Down

0 comments on commit cdbb8c7

Please sign in to comment.