Merge pull request #193 from SeaQL/codegen-col-unique-keys

Codegen parse column unique key from index
This commit is contained in:
Chris Tsang 2021-09-26 16:12:50 +08:00 committed by GitHub
commit be01782ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

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.2" } sea-query = { version = "^0.16.4" }
syn = { version = "^1", default-features = false, features = [ syn = { version = "^1", default-features = false, features = [
"derive", "derive",
"parsing", "parsing",

View File

@ -33,6 +33,17 @@ impl EntityTransformer {
.get_columns() .get_columns()
.iter() .iter()
.map(|col_def| col_def.into()) .map(|col_def| col_def.into())
.map(|mut col: Column| {
col.unique = table_create
.get_indexes()
.iter()
.filter(|index| index.is_unique_key())
.map(|index| index.get_index_spec().get_column_names())
.filter(|col_names| col_names.len() == 1 && col_names[0] == col.name)
.count()
> 0;
col
})
.collect(); .collect();
let relations = table_create let relations = table_create
.get_foreign_key_create_stmts() .get_foreign_key_create_stmts()

View File

@ -346,6 +346,9 @@ impl EntityWriter {
attrs.push(quote! { nullable }); attrs.push(quote! { nullable });
} }
}; };
if col.unique {
attrs.push(quote! { unique });
}
if !attrs.is_empty() { if !attrs.is_empty() {
let mut ts = TokenStream::new(); let mut ts = TokenStream::new();
for (i, attr) in attrs.into_iter().enumerate() { for (i, attr) in attrs.into_iter().enumerate() {