fix(dbml): resolve commented cross-file // Ref: lines

Commented refs are collected per file and resolved against the combined
model after all inputs are loaded (directory, --from-list, merge, jobs).
Matched refs become FKs and relationships; duplicates of existing FKs are
skipped; missing targets are skipped with a warning; column type
mismatches warn.

Also keep reused index names within a DBML table instead of overwriting,
give a second FK to the same table a distinct relationship name, and make
the pgsql writer match relationships to FKs by name first.
This commit is contained in:
2026-09-23 18:48:07 +02:00
parent b91985c493
commit 2f69205aa0
13 changed files with 745 additions and 20 deletions
+11 -6
View File
@@ -1259,13 +1259,18 @@ func (w *Writer) writeForeignKeys(schema *models.Schema) error {
fkName = fmt.Sprintf("fk_%s_%s", table.SQLName(), rel.ToTable)
}
// Find the foreign key constraint that matches this relationship
// Find the foreign key constraint that matches this relationship.
// Prefer the exact name: with several FKs to the same table, a
// referenced-table match alone picks an arbitrary one.
var fkConstraint *models.Constraint
for _, constraint := range table.Constraints {
if constraint.Type == models.ForeignKeyConstraint &&
(constraint.Name == fkName || constraint.ReferencedTable == rel.ToTable) {
fkConstraint = constraint
break
if c, ok := table.Constraints[fkName]; ok && c.Type == models.ForeignKeyConstraint {
fkConstraint = c
} else {
for _, constraint := range sortConstraints(table.Constraints) {
if constraint.Type == models.ForeignKeyConstraint && constraint.ReferencedTable == rel.ToTable {
fkConstraint = constraint
break
}
}
}