Skip to content

Commit

Permalink
Fix foreign field naming
Browse files Browse the repository at this point in the history
  • Loading branch information
vloldik committed Oct 26, 2024
1 parent 7dcbeec commit f5e0b69
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"program": ".",
"type": "go",
"cwd": "${cwd}",
"args": ["generate", "--module", "github.com/vloldik/dbml-gen", "-i", "./test/test.dbml", "-o", "./test/output", "--backend", "gorm"]
"args": ["generate", "--module", "github.com/vloldik/dbml-gen", "-i", "./example/test.dbml", "-o", "./example/output", "--backend", "gorm"]
}]
}
2 changes: 1 addition & 1 deletion example/output/ecommerce/merchant.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/output/migrates/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func MigrateAll(db *gorm.DB) error {
return db.AutoMigrate(&ecommerce.Order{}, &ecommerce.Product{}, &ecommerce.MerchantPeriod{}, &public.User{}, &public.Country{}, &ecommerce.ProductTag{}, &ecommerce.Merchant{}, &ecommerce.OrderItem{})
return db.AutoMigrate(&ecommerce.ProductTag{}, &ecommerce.MerchantPeriod{}, &ecommerce.Merchant{}, &public.Country{}, &ecommerce.Order{}, &public.User{}, &ecommerce.OrderItem{}, &ecommerce.Product{})
}
11 changes: 7 additions & 4 deletions internal/generator/gormgen/struct_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (sg *GORMStructGenerator) createFieldRelation(relation *models.Relationship
// True if we want to use []list
isX_ToMany := relation.RelationType == models.ManyToMany || relation.RelationType == models.OneToMany
qual := sg.getStructQualifier(relation.ToTable)
createdFieldName := sg.createRelatedFieldName(relation.ToField, relation.ToTable, isX_ToMany)
createdFieldName := sg.createRelatedFieldName(relation.FromField, relation.ToTable, isX_ToMany)

createdField := jen.Id(createdFieldName)
if isX_ToMany {
Expand Down Expand Up @@ -186,11 +186,14 @@ func (sg *GORMStructGenerator) getStructQualifier(table *models.Table) string {
return qual
}

func (sg *GORMStructGenerator) createRelatedFieldName(field *models.Field, table *models.Table, isX_toMany bool) string {
relatedFieldName := field.DisplayName()
func (sg *GORMStructGenerator) createRelatedFieldName(fromField *models.Field, toTable *models.Table, isX_toMany bool) string {
relatedFieldName := fromField.DisplayName()
relatedFieldName, found := strings.CutSuffix(relatedFieldName, "Id")
if !found {
relatedFieldName, found = strings.CutSuffix(relatedFieldName, "ID")
}
if len(relatedFieldName) < 2 || !found {
relatedFieldName = table.DisplayName()
relatedFieldName = toTable.DisplayName()
}
if sg.structFields.hasName(relatedFieldName) {
relatedFieldName = "Related" + relatedFieldName
Expand Down

0 comments on commit f5e0b69

Please sign in to comment.