Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ pub async fn run_generate_command(
sqlx_connect::<Sqlite>(max_connections, url.as_str(), None).await?;
println!("Discovering schema ...");
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
.tables
.into_iter()
Expand Down
11 changes: 11 additions & 0 deletions sea-orm-codegen/src/entity/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ impl EntityTransformer {
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 {
RelationType::HasOne
} else {
Expand Down
Loading