CLI: generate has_one relation for foreign key of unique index / constraint (#2254)

* CLI: generate `has_one` relation for foreign key of unique index / constraint

* Primary key column is unique

* Bump dependency

* Fix
This commit is contained in:
Billy Chan 2024-06-19 20:40:07 +08:00 committed by GitHub
parent b4506c0647
commit bca933a055
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -137,7 +137,10 @@ pub async fn run_generate_command(
sqlx_connect::<Sqlite>(max_connections, url.as_str(), None).await?; sqlx_connect::<Sqlite>(max_connections, url.as_str(), None).await?;
println!("Discovering schema ..."); println!("Discovering schema ...");
let schema_discovery = SchemaDiscovery::new(connection); let schema_discovery = SchemaDiscovery::new(connection);
let schema = schema_discovery.discover().await?; let schema = schema_discovery
.discover()
.await?
.merge_indexes_into_table();
let table_stmts = schema let table_stmts = schema
.tables .tables
.into_iter() .into_iter()

View File

@ -148,6 +148,17 @@ impl EntityTransformer {
break; break;
} }
} }
if rel.columns.len() == entity.primary_keys.len() {
let mut count_pk = 0;
for primary_key in entity.primary_keys.iter() {
if rel.columns.contains(&primary_key.name) {
count_pk += 1;
}
}
if count_pk == entity.primary_keys.len() {
unique = true;
}
}
let rel_type = if unique { let rel_type = if unique {
RelationType::HasOne RelationType::HasOne
} else { } else {