Skip to content

"does not exist" errors for simple joins #213

Closed

Description

@kyleconroy

Hi,
I'm seeing similar errors with a few variations on this join. Is this a related issue, or shall I raise a seperate one?

SELECT f, b
FROM foo f
JOIN bar b ON b.id = f.id
WHERE f.id = $1;

query.sql:6:8: column "f" does not exist

SELECT foo, bar
FROM foo
JOIN bar ON bar.id = foo.id
WHERE foo.id = $1;

query.sql:6:8: column "foo" does not exist

SELECT f.id, b.title
FROM foo f
JOIN bar b ON b.id = f.id
WHERE f.id = $1;
                
query.sql:6:8: column "id" does not exist

Originally posted by @tgascoigne in #159 (comment)

Activity

  1. kyleconroy commented on Dec 24, 2019

    @kyleconroy
    CollaboratorAuthor

    @tgascoigne I'm going to assume that the foo and bar tables look like this

    CREATE TABLE foo (
      id integer primary key
    );
    
    CREATE TABLE bar (
      id integer references foo(id),
      title text
    );
  2. tgascoigne commented on Dec 25, 2019

    @tgascoigne

    Yep, that's equivalent to what I'm using

  3. kyleconroy commented on Dec 25, 2019

    @kyleconroy
    CollaboratorAuthor

    There are two issues at play. The first is that sqlc doesn't currently support SELECT foo FROM foo. This is the reason the first two queries error. Do you have example Go code you use to scan those results?

    The third query fails due to incorrect alias support, and should be easier to fix.

  4. tgascoigne commented on Dec 25, 2019

    @tgascoigne

    Thanks for the quick response :)

    Unfortunately I don't have much more than the examples provided and the code generated by sqlc - I've just started playing around with it today for potential use in future.

    My hope was that I'd get something like the below struct generated, but I'd be happy to have any way to extract all columns from both tables:

    type SomeRow struct {
        Foo Foo
        Bar Bar
    }

    In the meantime I've worked around it by explicitly selecting each field I care about, which works perfectly fine:

    SELECT foo.id, bar.title
    FROM foo 
    JOIN bar ON bar.id = foo.id
    WHERE foo.id = $1;
  5. kyleconroy commented on Dec 25, 2019

    @kyleconroy
    CollaboratorAuthor

    but I'd be happy to have any way to extract all columns from both tables

    You can get that behavior by using *:

    SELECT *
    FROM foo 
    JOIN bar ON bar.id = foo.id
    WHERE foo.id = $1;

    Selecting the table name (SELECT foo FROM foo) returns a single column with a composite type ROW(id). Composite types have poor support in lib/pq. The value passed to Scan is tuple represented as a string (e.g. (id)). I haven't found a library that properly handles these values. SELECT * has much better support.

  6. tgascoigne commented on Dec 25, 2019

    @tgascoigne

    Now that you mention it I did try SELECT *, but both tables have an id field, and sqlc expands the * out to something like SELECT id, title, id (which postgres rejects due to ambiguity)

  7. kyleconroy commented on Dec 26, 2019

    @kyleconroy
    CollaboratorAuthor

    which postgres rejects due to ambiguity

    🤦‍♂ yeah, * expansion should be smart enough to generate columns that do not collide.

  8. kyleconroy commented on Feb 10, 2020

    @kyleconroy
    CollaboratorAuthor

    @tgascoigne This has now be fixed in master. The following two queries should now work

    SELECT f.id, b.title
    FROM foo f
    JOIN bar b ON b.id = f.id
    WHERE f.id = $1;
    
    SELECT *
    FROM foo 
    JOIN bar ON bar.id = foo.id
    WHERE foo.id = $1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions