Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query with LEFT JOIN doesn't create Null* fields #2044

Closed
Luit opened this issue Jan 31, 2023 · 1 comment · Fixed by #2051
Closed

Query with LEFT JOIN doesn't create Null* fields #2044

Luit opened this issue Jan 31, 2023 · 1 comment · Fixed by #2051
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@Luit
Copy link

Luit commented Jan 31, 2023

Version

1.15.0

What happened?

We've had an issue where a query we did a LEFT JOIN in didn't generate Null* fields in the *Row struct, resulting in the converting NULL to %s is unsupported error at runtime.

We found out this was fixed by putting an INNER JOIN that came after that LEFT JOIN before it. Somehow, query TestLeftInner generates a different struct than query TestInnerLeft:

type TestLeftInnerRow struct {
	A string
	B string
	C string
}

type TestInnerLeftRow struct {
	A string
	B string
	C sql.NullString
}

Relevant log output

No response

Database schema

CREATE TABLE a (
  id   BIGSERIAL PRIMARY KEY,
  a TEXT NOT NULL
);

CREATE TABLE b (
  id BIGSERIAL PRIMARY KEY,
  b TEXT NOT NULL,
  a_id BIGINT NOT NULL REFERENCES a (id)
);

CREATE TABLE c (
  id BIGSERIAL PRIMARY KEY,
  c TEXT NOT NULL,
  a_id BIGINT NOT NULL REFERENCES a (id)
);

SQL queries

-- name: TestLeftInner :many
SELECT a.a, b.b, c.c
FROM b
LEFT JOIN c ON c.a_id = a.id
INNER JOIN a ON b.a_id = a.id;

-- name: TestInnerLeft :many
SELECT a.a, b.b, c.c
FROM b
INNER JOIN a ON b.a_id = a.id
LEFT JOIN c ON c.a_id = a.id;

Configuration

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

Playground URL

https://play.sqlc.dev/p/56e981109a258f404058dd3cfe845c8fd0e0b195c0f3410479a819caea2c72ef

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL, MySQL

What type of code are you generating?

Go

@Luit Luit added bug Something isn't working triage New issues that hasn't been reviewed labels Jan 31, 2023
@haton14
Copy link

haton14 commented Feb 2, 2023

This also occurs in sqlite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants