Skip to content

Commit

Permalink
Fix selecting in one-to-many relatiosships
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
aarondl committed Jun 15, 2017
1 parent 1e97530 commit 35563d1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/vattle/sqlboiler/boilingcore"
)

const sqlBoilerVersion = "2.4.0"
const sqlBoilerVersion = "2.4.1"

var (
cmdState *boilingcore.State
Expand Down
5 changes: 5 additions & 0 deletions queries/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions templates/06_relationship_to_many.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand All @@ -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
}

Expand Down

0 comments on commit 35563d1

Please sign in to comment.