Merge pull request #157 from SeaQL/codegen-fixup

Codegen include `nullable` attribute when `column_type` is explicitly defined
This commit is contained in:
Chris Tsang 2021-09-15 13:07:51 +08:00 committed by GitHub
commit 38a4d3a810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 17 additions and 11 deletions

View File

@ -22,7 +22,7 @@ clap = { version = "^2.33.3" }
dotenv = { version = "^0.15" } dotenv = { version = "^0.15" }
async-std = { version = "^1.9", features = [ "attributes" ] } async-std = { version = "^1.9", features = [ "attributes" ] }
sea-orm-codegen = { version = "^0.2.0", path = "../sea-orm-codegen" } sea-orm-codegen = { version = "^0.2.0", path = "../sea-orm-codegen" }
sea-schema = { version = "^0.2.7", git = "https://github.com/SeaQL/sea-schema.git", branch = "update-sea-query-version", default-features = false, features = [ sea-schema = { version = "^0.2.7", git = "https://github.com/SeaQL/sea-schema.git", default-features = false, features = [
"sqlx-mysql", "sqlx-mysql",
"sqlx-postgres", "sqlx-postgres",
"discovery", "discovery",

View File

@ -15,7 +15,7 @@ name = "sea_orm_codegen"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
sea-query = { version = "^0.16.1", git = "https://github.com/SeaQL/sea-query.git", branch = "foreign-key-getters" } sea-query = { version = "^0.16.2" }
syn = { version = "^1", default-features = false, features = [ syn = { version = "^1", default-features = false, features = [
"derive", "derive",
"parsing", "parsing",

View File

@ -1,3 +1,4 @@
ignore = [ ignore = [
"tests/entity/*.rs", "tests/compact/*.rs",
"tests/expanded/*.rs",
] ]

View File

@ -2,7 +2,6 @@ use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, TokenStream}; use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use sea_query::{ColumnDef, ColumnSpec, ColumnType}; use sea_query::{ColumnDef, ColumnSpec, ColumnType};
use syn::punctuated::Punctuated;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Column { pub struct Column {
@ -59,6 +58,7 @@ impl Column {
ColumnType::Double(Some(l)) => Some(format!("Double(Some({}))", l)), ColumnType::Double(Some(l)) => Some(format!("Double(Some({}))", l)),
ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)), ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)),
ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)), ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)),
ColumnType::Text => Some("Text".to_owned()),
ColumnType::Custom(iden) => { ColumnType::Custom(iden) => {
Some(format!("Custom(\"{}\".to_owned())", iden.to_string())) Some(format!("Custom(\"{}\".to_owned())", iden.to_string()))
} }

View File

@ -1,7 +1,6 @@
use crate::Entity; use crate::Entity;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
use std::iter::FromIterator;
use syn::{punctuated::Punctuated, token::Comma}; use syn::{punctuated::Punctuated, token::Comma};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -343,6 +342,9 @@ impl EntityWriter {
} }
if let Some(ts) = col.get_col_type_attrs() { if let Some(ts) = col.get_col_type_attrs() {
attrs.extend(vec![ts]); attrs.extend(vec![ts]);
if !col.not_null {
attrs.push(quote! { nullable });
}
}; };
if !attrs.is_empty() { if !attrs.is_empty() {
let mut ts = TokenStream::new(); let mut ts = TokenStream::new();
@ -411,9 +413,9 @@ mod tests {
}, },
Column { Column {
name: "name".to_owned(), name: "name".to_owned(),
col_type: ColumnType::String(Some(255)), col_type: ColumnType::Text,
auto_increment: false, auto_increment: false,
not_null: true, not_null: false,
unique: false, unique: false,
}, },
], ],

View File

@ -7,7 +7,8 @@ use sea_orm::entity::prelude::*;
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,
pub name: String, #[sea_orm(column_type = "Text", nullable)]
pub name: Option<String> ,
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

View File

@ -14,7 +14,7 @@ impl EntityName for Entity {
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: Option<String> ,
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
@ -46,7 +46,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef { fn def(&self) -> ColumnDef {
match self { match self {
Self::Id => ColumnType::Integer.def(), Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(), Self::Name => ColumnType::Text.def().null(),
} }
} }
} }

View File

@ -163,7 +163,9 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
"bool" => quote! { Boolean }, "bool" => quote! { Boolean },
"NaiveDate" => quote! { Date }, "NaiveDate" => quote! { Date },
"NaiveTime" => quote! { Time }, "NaiveTime" => quote! { Time },
"DateTime" | "NaiveDateTime" | "DateTimeWithTimeZone" => quote! { DateTime }, "DateTime" | "NaiveDateTime" | "DateTimeWithTimeZone" => {
quote! { DateTime }
}
"Uuid" => quote! { Uuid }, "Uuid" => quote! { Uuid },
"Json" => quote! { Json }, "Json" => quote! { Json },
"Decimal" => quote! { Decimal }, "Decimal" => quote! { Decimal },