Skip to content

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

Description

@Luit

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

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Jan 31, 2023
  2. haton14 commented on Feb 2, 2023

    @haton14

    This also occurs in sqlite.

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

    bugSomething isn't workingtriageNew issues that hasn't been reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions