sqlc-dev / sqlc

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

sqlc.embed(<right_join_table>) should generate NULLable fields in LEFT/RIGHT JOIN #3240

Open lordvidex opened 8 months ago

lordvidex commented 8 months ago

Version

1.25.0

What happened?

I have some table A and I made a LEFT JOIN on table B, but i used the sqlc.embed(b) on the table that I joined and unfortunately, I got non-nullable types. The table that is OUTER JOINED should be always nullable but it seems the sqlc.embed somehow doesn't support this yet.

I worked around this by removing sqlc.embed for now, but the embed feature is really useful for my usecase.

Relevant log output

No response

Database schema

CREATE TABLE IF NOT EXISTS workflow(
   id BIGSERIAL PRIMARY KEY,
   org_id BIGINT NOT NULL,
   name TEXT NOT NULL,
   description TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS step (
    id BIGSERIAL PRIMARY KEY,
    workflow_id BIGINT NOT NULL REFERENCES workflow(id) ON DELETE CASCADE,
    index INT NOT NULL,
    title TEXT NOT NULL,
    description TEXT NOT NULL
);

SQL queries

SELECT wf.*, sqlc.embed(s) FROM (
    SELECT * from workflow w WHERE w.id = $1 LIMIT 1) wf 
LEFT JOIN step s ON s.workflow_id = wf.id

Configuration

No response

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

sourcec0de commented 8 months ago

I ran into this just now. A related consequence is that you end up with scan errors like this.

converting NULL to string is unsupported
nikoprotic commented 5 months ago

I just took down prod so I really could have used this.