diff --git a/templates/06_relationship_to_many.tpl b/templates/06_relationship_to_many.tpl index ee0a87c..5748d06 100644 --- a/templates/06_relationship_to_many.tpl +++ b/templates/06_relationship_to_many.tpl @@ -6,13 +6,13 @@ {{- $varNameSingular := .ForeignTable | singular | camelCase -}} {{- $rel := txtsFromToMany $dot.Tables $table . -}} {{- $schemaForeignTable := .ForeignTable | $dot.SchemaTable -}} -// {{$rel.Function.Name}}G retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} +// {{$rel.Function.Name}}G retrieves all the {{.ForeignTable | singular}}'s {{$rel.ForeignTable.NameHumanReadable}} {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. func (o *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query { return o.{{$rel.Function.Name}}(boil.GetDB(), mods...) } -// {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor +// {{$rel.Function.Name}} retrieves all the {{.ForeignTable | singular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. func (o *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query { queryMods := []qm.QueryMod{ diff --git a/templates_test/relationship_to_many.tpl b/templates_test/relationship_to_many.tpl index ad3c7ce..c779352 100644 --- a/templates_test/relationship_to_many.tpl +++ b/templates_test/relationship_to_many.tpl @@ -5,6 +5,7 @@ {{- range .Table.ToManyRelationships -}} {{- $txt := txtsFromToMany $dot.Tables $table .}} {{- $varNameSingular := .Table | singular | camelCase -}} + {{- $foreignVarNameSingular := .ForeignTable | singular | camelCase -}} func test{{$txt.LocalTable.NameGo}}ToMany{{$txt.Function.Name}}(t *testing.T) { var err error tx := MustTx(boil.Begin()) @@ -22,8 +23,8 @@ func test{{$txt.LocalTable.NameGo}}ToMany{{$txt.Function.Name}}(t *testing.T) { t.Fatal(err) } - randomize.Struct(seed, &b, {{$txt.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $txt.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) - randomize.Struct(seed, &c, {{$txt.ForeignTable.NameSingular | camelCase}}DBTypes, false{{if not $txt.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) + randomize.Struct(seed, &b, {{$foreignVarNameSingular}}DBTypes, false{{if not $txt.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) + randomize.Struct(seed, &c, {{$foreignVarNameSingular}}DBTypes, false{{if not $txt.Function.UsesBytes}}, "{{.ForeignColumn}}"{{end}}) {{if .Nullable -}} a.{{.Column | titleCase}}.Valid = true {{- end}} diff --git a/text_helpers.go b/text_helpers.go index 1e78eed..c340338 100644 --- a/text_helpers.go +++ b/text_helpers.go @@ -109,13 +109,11 @@ func txtsFromOneToOne(tables []bdb.Table, table bdb.Table, oneToOne bdb.ToOneRel type TxtToMany struct { LocalTable struct { NameGo string - NameSingular string ColumnNameGo string } ForeignTable struct { NameGo string - NameSingular string NamePluralGo string NameHumanReadable string ColumnNameGo string @@ -137,15 +135,14 @@ type TxtToMany struct { // transformation in advance for a given relationship. func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelationship) TxtToMany { r := TxtToMany{} - r.LocalTable.NameSingular = strmangle.Singular(table.Name) - r.LocalTable.NameGo = strmangle.TitleCase(r.LocalTable.NameSingular) + r.LocalTable.NameGo = strmangle.TitleCase(strmangle.Singular(table.Name)) r.LocalTable.ColumnNameGo = strmangle.TitleCase(rel.Column) - r.ForeignTable.NameSingular = strmangle.Singular(rel.ForeignTable) + foreignNameSingular := strmangle.Singular(rel.ForeignTable) r.ForeignTable.NamePluralGo = strmangle.TitleCase(strmangle.Plural(rel.ForeignTable)) - r.ForeignTable.NameGo = strmangle.TitleCase(r.ForeignTable.NameSingular) + r.ForeignTable.NameGo = strmangle.TitleCase(foreignNameSingular) r.ForeignTable.ColumnNameGo = strmangle.TitleCase(rel.ForeignColumn) - r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(r.ForeignTable.NameSingular)) + r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(foreignNameSingular)) r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1) r.Function.Name, r.Function.ForeignName = txtNameToMany(rel) diff --git a/text_helpers_test.go b/text_helpers_test.go index 070d391..3d494b9 100644 --- a/text_helpers_test.go +++ b/text_helpers_test.go @@ -125,11 +125,9 @@ func TestTxtsFromMany(t *testing.T) { texts := txtsFromToMany(tables, pilots, pilots.ToManyRelationships[0]) expect := TxtToMany{} expect.LocalTable.NameGo = "Pilot" - expect.LocalTable.NameSingular = "pilot" expect.LocalTable.ColumnNameGo = "ID" expect.ForeignTable.NameGo = "License" - expect.ForeignTable.NameSingular = "license" expect.ForeignTable.NamePluralGo = "Licenses" expect.ForeignTable.NameHumanReadable = "licenses" expect.ForeignTable.ColumnNameGo = "PilotID" @@ -147,11 +145,9 @@ func TestTxtsFromMany(t *testing.T) { texts = txtsFromToMany(tables, pilots, pilots.ToManyRelationships[1]) expect = TxtToMany{} expect.LocalTable.NameGo = "Pilot" - expect.LocalTable.NameSingular = "pilot" expect.LocalTable.ColumnNameGo = "ID" expect.ForeignTable.NameGo = "Language" - expect.ForeignTable.NameSingular = "language" expect.ForeignTable.NamePluralGo = "Languages" expect.ForeignTable.NameHumanReadable = "languages" expect.ForeignTable.ColumnNameGo = "ID"