From 0eccbc9143ae1d559cdec9fc6e5497df5a999d75 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 24 Nov 2022 12:15:31 +0800 Subject: [PATCH] [sea-orm-cli] generate entity with relation variant order by name of reference table (#1229) --- sea-orm-codegen/src/entity/transformer.rs | 20 +++++++++++++------- sea-orm-codegen/src/entity/writer.rs | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/sea-orm-codegen/src/entity/transformer.rs b/sea-orm-codegen/src/entity/transformer.rs index 3fb553db0..c456fe179 100644 --- a/sea-orm-codegen/src/entity/transformer.rs +++ b/sea-orm-codegen/src/entity/transformer.rs @@ -3,17 +3,17 @@ use crate::{ PrimaryKey, Relation, RelationType, }; use sea_query::{ColumnSpec, TableCreateStatement}; -use std::collections::HashMap; +use std::collections::BTreeMap; #[derive(Clone, Debug)] pub struct EntityTransformer; impl EntityTransformer { pub fn transform(table_create_stmts: Vec) -> Result { - let mut enums: HashMap = HashMap::new(); - let mut inverse_relations: HashMap> = HashMap::new(); - let mut conjunct_relations: HashMap> = HashMap::new(); - let mut entities = HashMap::new(); + let mut enums: BTreeMap = BTreeMap::new(); + let mut inverse_relations: BTreeMap> = BTreeMap::new(); + let mut conjunct_relations: BTreeMap> = BTreeMap::new(); + let mut entities = BTreeMap::new(); for table_create in table_create_stmts.into_iter() { let table_name = match table_create.get_table_name() { Some(table_ref) => match table_ref { @@ -71,7 +71,7 @@ impl EntityTransformer { col }) .collect(); - let mut ref_table_counts: HashMap = HashMap::new(); + let mut ref_table_counts: BTreeMap = BTreeMap::new(); let relations: Vec = table_create .get_foreign_key_create_stmts() .iter() @@ -202,7 +202,13 @@ impl EntityTransformer { } } Ok(EntityWriter { - entities: entities.into_iter().map(|(_, v)| v).collect(), + entities: entities + .into_iter() + .map(|(_, mut v)| { + v.relations.sort_by(|a, b| a.ref_table.cmp(&b.ref_table)); + v + }) + .collect(), enums, }) } diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 360c13fc1..6b62cac13 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -2,14 +2,14 @@ use crate::{util::escape_rust_keyword, ActiveEnum, Entity}; use heck::CamelCase; use proc_macro2::TokenStream; use quote::{format_ident, quote}; -use std::{collections::HashMap, str::FromStr}; +use std::{collections::BTreeMap, str::FromStr}; use syn::{punctuated::Punctuated, token::Comma}; use tracing::info; #[derive(Clone, Debug)] pub struct EntityWriter { pub(crate) entities: Vec, - pub(crate) enums: HashMap, + pub(crate) enums: BTreeMap, } pub struct WriterOutput {