Skip to content

sqlc.embed fails with LEFT JOIN #2634

New issue

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

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

Already on GitHub? # to your account

Closed
hectorj-thetreep opened this issue Aug 18, 2023 · 3 comments
Closed

sqlc.embed fails with LEFT JOIN #2634

hectorj-thetreep opened this issue Aug 18, 2023 · 3 comments
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@hectorj-thetreep
Copy link

Version

1.20.0

What happened?

The generated code

func (q *Queries) GetA(ctx context.Context) (GetARow, error) {
	row := q.db.QueryRowContext(ctx, getA)
	var i GetARow
	err := row.Scan(
		&i.A.ID,
		&i.A.Name,
		&i.B.ID,
		&i.B.AID,
		&i.B.Label,
	)
	return i, err
}

will fail when there is no B to join to A, because it did not detect that the whole i.B is nullable.

Relevant log output

sql: Scan error on column index 2, name "id": converting NULL to int32 is unsupported

Database schema

CREATE TABLE A (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL
);

CREATE TABLE B (
  id BIGSERIAL PRIMARY KEY,
  a_id BIGINT NOT NULL,
  label text NOT NULL
);

SQL queries

SELECT sqlc.embed(A), sqlc.embed(B) FROM A LEFT JOIN B ON A.id = B.a_id LIMIT 1;

Configuration

No response

Playground URL

https://play.sqlc.dev/p/c045f5e33caf61f3e913a742f08518a167c247c4e5902a995f17ee200a553b2a](https://play.sqlc.dev/p/a6840c57f06a38387e9af1604059bb1db09e0db71f7a462c7bcd6a8dcfc42043

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@hectorj-thetreep hectorj-thetreep added bug Something isn't working triage New issues that hasn't been reviewed labels Aug 18, 2023
@hectorj-thetreep
Copy link
Author

hectorj-thetreep commented Aug 18, 2023

Ideally I would like something like this:

type NullableB {
	B B
	Valid bool
}
type GetARow struct {
	A A
	B NullableB
}

But it might be tricky to Scan.

An alternative is to have NullableB be a copy of B with all its fields nullable… not ideal but maybe easier.

@nickjackson
Copy link
Contributor

this is a duplicate of #2348 and #2472

@hectorj-thetreep
Copy link
Author

hectorj-thetreep commented Aug 18, 2023 via email

@hectorj-thetreep hectorj-thetreep closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

2 participants