From 59c4a5ebf896c17e0e67e5ddb5b5a0ab43988daa Mon Sep 17 00:00:00 2001 From: Hein Date: Sun, 8 Feb 2026 15:20:20 +0200 Subject: [PATCH] test(writer): enhance has-many relationship tests with join tag verification --- pkg/writers/bun/type_mapper.go | 10 +++++++++- pkg/writers/bun/writer_test.go | 16 +++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/pkg/writers/bun/type_mapper.go b/pkg/writers/bun/type_mapper.go index d8b9216..4cbbd83 100644 --- a/pkg/writers/bun/type_mapper.go +++ b/pkg/writers/bun/type_mapper.go @@ -251,7 +251,15 @@ func (tm *TypeMapper) BuildRelationshipTag(constraint *models.Constraint, relTyp if len(constraint.Columns) > 0 && len(constraint.ReferencedColumns) > 0 { localCol := constraint.Columns[0] foreignCol := constraint.ReferencedColumns[0] - parts = append(parts, fmt.Sprintf("join:%s=%s", localCol, foreignCol)) + + // For has-many relationships, swap the columns + // has-one: join:fk_in_this_table=pk_in_other_table + // has-many: join:pk_in_this_table=fk_in_other_table + if relType == "has-many" { + parts = append(parts, fmt.Sprintf("join:%s=%s", foreignCol, localCol)) + } else { + parts = append(parts, fmt.Sprintf("join:%s=%s", localCol, foreignCol)) + } } return strings.Join(parts, ",") diff --git a/pkg/writers/bun/writer_test.go b/pkg/writers/bun/writer_test.go index bae0b1e..c6c7a66 100644 --- a/pkg/writers/bun/writer_test.go +++ b/pkg/writers/bun/writer_test.go @@ -308,14 +308,20 @@ func TestWriter_MultipleReferencesToSameTable(t *testing.T) { filepointerStr := string(filepointerContent) // Should have two different has-many relationships with unique names - hasManyExpectations := []string{ - "RelRIDFilepointerRequestOrgAPIEvents", // Has many via rid_filepointer_request - "RelRIDFilepointerResponseOrgAPIEvents", // Has many via rid_filepointer_response + hasManyExpectations := []struct { + fieldName string + tag string + }{ + {"RelRIDFilepointerRequestOrgAPIEvents", "join:id_filepointer=rid_filepointer_request"}, // Has many via rid_filepointer_request + {"RelRIDFilepointerResponseOrgAPIEvents", "join:id_filepointer=rid_filepointer_response"}, // Has many via rid_filepointer_response } for _, exp := range hasManyExpectations { - if !strings.Contains(filepointerStr, exp) { - t.Errorf("Missing has-many relationship field: %s\nGenerated:\n%s", exp, filepointerStr) + if !strings.Contains(filepointerStr, exp.fieldName) { + t.Errorf("Missing has-many relationship field: %s\nGenerated:\n%s", exp.fieldName, filepointerStr) + } + if !strings.Contains(filepointerStr, exp.tag) { + t.Errorf("Missing has-many relationship join tag: %s\nGenerated:\n%s", exp.tag, filepointerStr) } } }