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

LEFT JOIN not generating Null* types with table aliases #1897

Closed
fr3fou opened this issue Oct 13, 2022 · 3 comments · Fixed by #2873
Closed

LEFT JOIN not generating Null* types with table aliases #1897

fr3fou opened this issue Oct 13, 2022 · 3 comments · Fixed by #2873

Comments

@fr3fou
Copy link

fr3fou commented Oct 13, 2022

Version

1.15.0

What happened?

sqlc generates incorrect code when using table aliases inside left joins.
#1491 was supposed to fix it, but I'm still able to reproduce it.
The StripeID field should be of type sql.NullString but it isn't.

Relevant log output

No response

Database schema

CREATE TABLE users (
	user_id uuid primary key  
);

CREATE TABLE user_stripe_customers
(
    user_id   uuid NOT NULL,
    stripe_id TEXT NOT NULL,
    UNIQUE (user_id, stripe_id)
);
ALTER TABLE user_stripe_customers
    ADD FOREIGN KEY (user_id) REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE;

SQL queries

-- name: GetStripeUserByUserID :one
SELECT stripe_id, users.user_id
FROM users
         LEFT JOIN user_stripe_customers ON users.user_id = user_stripe_customers.user_id
WHERE users.user_id = @user_id;


-- name: GetStripeUserByUserIDBroken :one
SELECT stripe_id, u.user_id
FROM users u
         LEFT JOIN user_stripe_customers usu ON u.user_id = usu.user_id
WHERE u.user_id = @user_id;

Configuration

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

Playground URL

https://play.sqlc.dev/p/4827a9efa8fd793d7ee704f877890af04675aa382a3942d0f9807a95ae779cf7

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@fr3fou fr3fou added bug Something isn't working triage New issues that hasn't been reviewed labels Oct 13, 2022
@fr3fou fr3fou changed the title Left join issue with table aliases LEFT JOIN not generating Null* types with table aliases Oct 13, 2022
@kyleconroy kyleconroy added 📚 postgresql 💻 darwin 🔧 golang and removed triage New issues that hasn't been reviewed labels Nov 9, 2022
@talksik
Copy link

talksik commented Nov 9, 2022

I also found a case where when I left joined table a to b, the generated model for the b columns were not following the types of actual schema model b.
For instance ID for table b is NOT NULL, but the select query a., b. makes all of the b.* results become nullable.

nevermind, I'm dumb, left join implies right side is nullable duh

@talksik
Copy link

talksik commented Nov 9, 2022

nevermind actually, it does this for all joins.

@talksik
Copy link

talksik commented Nov 9, 2022

I found that sqlc doesn't consider primary key to be NOT NULL. I had to explicitly put it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants