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:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user