From 35563d1bdfe5cef270480db37088c70a919c1102 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Wed, 14 Jun 2017 20:53:39 -0700 Subject: [PATCH] Fix selecting in one-to-many relatiosships - This fix checks the query that's being prepared for any select statements, if there's none then add one so the query does what's expected. - Fix #159 --- main.go | 2 +- queries/query.go | 5 +++++ templates/06_relationship_to_many.tpl | 10 ++++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index ca01cf740..65e46f644 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( "github.com/vattle/sqlboiler/boilingcore" ) -const sqlBoilerVersion = "2.4.0" +const sqlBoilerVersion = "2.4.1" var ( cmdState *boilingcore.State diff --git a/queries/query.go b/queries/query.go index 8c2abdb3a..06f06a34f 100644 --- a/queries/query.go +++ b/queries/query.go @@ -188,6 +188,11 @@ func SetSelect(q *Query, sel []string) { q.selectCols = sel } +// GetSelect from the query +func GetSelect(q *Query) []string { + return q.selectCols +} + // SetCount on the query. func SetCount(q *Query) { q.count = true diff --git a/templates/06_relationship_to_many.tpl b/templates/06_relationship_to_many.tpl index 494461c42..47c71b28d 100644 --- a/templates/06_relationship_to_many.tpl +++ b/templates/06_relationship_to_many.tpl @@ -15,10 +15,7 @@ func (o *{{$txt.LocalTable.NameGo}}) {{$txt.Function.Name}}G(mods ...qm.QueryMod // {{$txt.Function.Name}} retrieves all the {{.ForeignTable | singular}}'s {{$txt.ForeignTable.NameHumanReadable}} with an executor {{- if not (eq $txt.Function.Name $txt.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. func (o *{{$txt.LocalTable.NameGo}}) {{$txt.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query { - queryMods := []qm.QueryMod{ - qm.Select("{{$schemaForeignTable}}.*"), - } - + var queryMods []qm.QueryMod if len(mods) != 0 { queryMods = append(queryMods, mods...) } @@ -37,6 +34,11 @@ func (o *{{$txt.LocalTable.NameGo}}) {{$txt.Function.Name}}(exec boil.Executor, query := {{$txt.ForeignTable.NamePluralGo}}(exec, queryMods...) queries.SetFrom(query.Query, "{{$schemaForeignTable}}") + + if len(queries.GetSelect(query.Query)) == 0 { + queries.SetSelect(query.Query, []string{"{{$schemaForeignTable}}.*"}) + } + return query }