Updated tests
Some checks failed
CI / Test (1.24) (push) Successful in -24m27s
CI / Test (1.25) (push) Successful in -24m28s
CI / Build (push) Successful in -25m56s
CI / Lint (push) Failing after -25m35s
Integration Tests / Integration Tests (push) Failing after -25m38s

This commit is contained in:
2025-12-28 14:35:20 +02:00
parent beb5b4fac8
commit 2a271b9859
3 changed files with 81 additions and 27 deletions

View File

@@ -382,6 +382,23 @@ func (r *Reader) isRelationship(tag string) bool {
return strings.Contains(tag, "bun:\"rel:") || strings.Contains(tag, ",rel:")
}
// getRelationType extracts the relationship type from a bun tag
func (r *Reader) getRelationType(bunTag string) string {
if strings.Contains(bunTag, "rel:has-many") {
return "has-many"
}
if strings.Contains(bunTag, "rel:belongs-to") {
return "belongs-to"
}
if strings.Contains(bunTag, "rel:has-one") {
return "has-one"
}
if strings.Contains(bunTag, "rel:many-to-many") {
return "many-to-many"
}
return ""
}
// parseRelationshipConstraints parses relationship fields to extract foreign key constraints
func (r *Reader) parseRelationshipConstraints(table *models.Table, structType *ast.StructType, structMap map[string]*models.Table) {
for _, field := range structType.Fields.List {
@@ -409,27 +426,50 @@ func (r *Reader) parseRelationshipConstraints(table *models.Table, structType *a
}
// Parse the join information: join:user_id=id
// This means: referencedTable.user_id = thisTable.id
// This means: thisTable.user_id = referencedTable.id
joinInfo := r.parseJoinInfo(bunTag)
if joinInfo == nil {
continue
}
// The FK is on the referenced table
// Determine which table gets the FK based on relationship type
relType := r.getRelationType(bunTag)
var fkTable *models.Table
var fkColumn, refTable, refColumn string
if relType == "belongs-to" {
// For belongs-to: FK is on the current table
// join:user_id=id means table.user_id references referencedTable.id
fkTable = table
fkColumn = joinInfo.ForeignKey
refTable = referencedTable.Name
refColumn = joinInfo.ReferencedKey
} else if relType == "has-many" {
// For has-many: FK is on the referenced table
// join:id=user_id means referencedTable.user_id references table.id
fkTable = referencedTable
fkColumn = joinInfo.ReferencedKey
refTable = table.Name
refColumn = joinInfo.ForeignKey
} else {
continue
}
constraint := &models.Constraint{
Name: fmt.Sprintf("fk_%s_%s", referencedTable.Name, table.Name),
Name: fmt.Sprintf("fk_%s_%s", fkTable.Name, refTable),
Type: models.ForeignKeyConstraint,
Table: referencedTable.Name,
Schema: referencedTable.Schema,
Columns: []string{joinInfo.ForeignKey},
ReferencedTable: table.Name,
ReferencedSchema: table.Schema,
ReferencedColumns: []string{joinInfo.ReferencedKey},
Table: fkTable.Name,
Schema: fkTable.Schema,
Columns: []string{fkColumn},
ReferencedTable: refTable,
ReferencedSchema: fkTable.Schema,
ReferencedColumns: []string{refColumn},
OnDelete: "NO ACTION", // Bun doesn't specify this in tags
OnUpdate: "NO ACTION",
}
referencedTable.Constraints[constraint.Name] = constraint
fkTable.Constraints[constraint.Name] = constraint
}
}