sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.64k stars 813 forks source link

edited query syntax is invalid, when using embed #3015

Open Nikola-Milovic opened 1 year ago

Nikola-Milovic commented 1 year ago

Version

1.24.0

What happened?

Trying to use embed, but I am getting some weird error about edited query syntax being invalid, but no pointers as to what's happening. I saw some issues related to NULL and LEFT JOINs but not sure what is happening in my queries.

Relevant log output

# package db
db/queries/projects.sql:2:18: edited query syntax is invalid: syntax error at or near ","
make: *** [Makefile:73: db/gen] Error 1

### Database schema

```sql
CREATE TABLE users (
    id uuid PRIMARY KEY,
    confirmed boolean NOT NULL DEFAULT false,
    full_name text,
    created_at timestamptz DEFAULT now(),
    updated_at timestamptz DEFAULT now()
);

CREATE TABLE teams (
    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
        display_name text NOT NULL,
        created_at timestamptz DEFAULT now()
);

CREATE TABLE user_team_membership (
    user_id uuid REFERENCES users(id),
    team_id uuid REFERENCES teams(id),
    created_at timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY (user_id, team_id)
);

CREATE TABLE projects (
    id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    updated_at timestamptz DEFAULT now()
);

SQL queries

-- name: FindAllUserProjects :many
SELECT sqlc.embed(projects), sqlc.embed(users), sqlc.embed(user_team_membership)
--SELECT * <- this works
FROM user_team_membership as utm
JOIN projects as p on utm.user_id = p.user_id
JOIN users as u on utm.user_id = u.id
WHERE utm.user_id = $1;

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "db/queries"
    schema: "db/schemas"
    gen:
      go:
        package: "db"
        sql_package: "pgx/v5"
        out: "internal/db"

Playground URL

https://play.sqlc.dev/p/2271bc8399f2303d47ee013bf2a3f932f595682859671163af34c450b356105b

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

reddiridabl666 commented 8 months ago

I've just encountered the same error, but it got fixed when I removed table aliases

levlozhkin commented 3 months ago

Propagating the table aliases to the sqlc.embed calls also seems to work. Try:

-- name: FindAllUserProjects :many
SELECT sqlc.embed(p), sqlc.embed(u), sqlc.embed(utm)
FROM user_team_membership as utm
JOIN projects as p on utm.user_id = p.user_id
JOIN users as u on utm.user_id = u.id
WHERE utm.user_id = $1;