Skip to content

Commit 78a6128

Browse files
authored
Fix incorrect ambiguous warning (sqlc-dev#57)
1 parent f99d842 commit 78a6128

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

internal/dinosql/parser.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,15 +714,16 @@ func resolveCatalogRefs(c core.Catalog, rvs []nodes.RangeVar, args []paramRef) (
714714
panic("too many field items: " + strconv.Itoa(len(items)))
715715
}
716716

717-
var search []string
718-
if table, ok := aliasMap[alias]; ok {
719-
search = append(search, table)
720-
} else {
721-
search = tables
717+
search := tables
718+
if alias != "" {
719+
search = []string{alias}
722720
}
723721

724722
var found int
725723
for _, table := range search {
724+
if original, ok := aliasMap[table]; ok {
725+
table = original
726+
}
726727
if c, ok := typeMap[table][key]; ok {
727728
found += 1
728729
a = append(a, Parameter{

internal/dinosql/query_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,27 @@ func TestQueries(t *testing.T) {
257257
Params: []Parameter{{1, core.Column{Name: "id", DataType: "serial", NotNull: true}}},
258258
},
259259
},
260+
{
261+
"table-name",
262+
`
263+
CREATE TABLE bar (id serial not null);
264+
CREATE TABLE foo (id serial not null, bar serial references bar(id));
265+
266+
SELECT foo.id
267+
FROM foo
268+
JOIN bar ON foo.bar = bar.id
269+
WHERE bar.id = $1 AND foo.id = $2;
270+
`,
271+
Query{
272+
Columns: []core.Column{
273+
{Name: "id", DataType: "serial", NotNull: true},
274+
},
275+
Params: []Parameter{
276+
{1, core.Column{Name: "id", DataType: "serial", NotNull: true}},
277+
{2, core.Column{Name: "id", DataType: "serial", NotNull: true}},
278+
},
279+
},
280+
},
260281
{
261282
"star",
262283
`

0 commit comments

Comments
 (0)