Updated tests
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user