sqlc-dev / sqlc

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

Sqlite engine creates wrong number of arguments in case of several JOINS with parameters #2368

Closed leodeim closed 1 year ago

leodeim commented 1 year ago

Version

1.18.0

What happened?

If you have a query with two JOINs which have parameters sqlite engine skips parameter in one of the JOINs. If you change sqlite to mysql generation works correctly. Schema, query and playground link are provided.

Relevant log output

No response

Database schema

CREATE TABLE authors (
  id   integer PRIMARY KEY,
  name text      NOT NULL,
  age  integer
);

CREATE TABLE translators (
  id   integer PRIMARY KEY,
  name text      NOT NULL,
  age  integer
);

CREATE TABLE books (
  id   integer PRIMARY KEY,
  author text      NOT NULL,
  translator text      NOT NULL,
  year  integer
);

SQL queries

DELETE FROM
  books AS b
WHERE
  b.author NOT IN (
    SELECT
      a.name
    FROM
      authors a
    WHERE
      a.age >= ?
  )
  AND b.translator NOT IN (
    SELECT
      t.name
    FROM
      translators t
    WHERE
      t.age >= ?
  )
  AND b.year <= ?;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "sqlite",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/b46d0f5491464a5698aeb56d290764f8a38daa6f59d2a5f89f3ab707a9b2b078

What operating system are you using?

Windows

What database engines are you using?

SQLite

What type of code are you generating?

Go

leodeim commented 1 year ago

sqlite generates:

type DeleteAuthorParams struct {
    Age  sql.NullInt64
    Year sql.NullInt64
}

mysql generates:

type DeleteAuthorParams struct {
    Age   sql.NullInt32
    Age_2 sql.NullInt32
    Year  sql.NullInt32
}
tape3093 commented 1 year ago

Same problem, please help.