-
Notifications
You must be signed in to change notification settings - Fork 773
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
Default Go types for nullable JSONB/UUID cannot handle null #745
Comments
I experienced the same issue. Working with cockroachDB my primary keys are v4 UUID's. Having these types not nullable prevents me from using the UUID generator that is provided by cockroachDB. Thanks for providing a work around @soroushj , I will use that for now. EDIT: I experienced that |
@clstb I was not able to reproduce this issue using sqlc v1.6.0. There might be a mistake (e.g. bad indentation) in your YAML file. This is the YAML equivalent of the sample JSON config above: version: '1'
packages: []
overrides:
- go_type: github.com/soroushj/sqlt.NullRawMessage
db_type: jsonb
nullable: true
- go_type: github.com/soroushj/sqlt.NullUUID
db_type: uuid
nullable: true |
I am also using
The corresponding schema is: CREATE TABLE IF NOT EXISTS accounts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name string NOT NULL
); I am running Big Sur v11.1. The concrete behaviour is that sqlc used the google uuid package in generated code. When omitting Also I managed to fix my problem by omitting the primary key in creation queries, which is a cleaner solution for me at the moment. |
@clstb In your case, that is the expected behavior. In PostgreSQL, |
That makes sense, thank you for clarification 👍 |
The default Go types for nullable JSONB (
json.RawMessage
) and nullable UUID (uuid.UUID
) cannot handle null. Using null with the former causes an error, while the latter cannot differentiate between zero value and null.The example below demonstrates these issues. (Tested with sqlc v1.5.0, PostgreSQL v12,
github.com/lib/pq
v1.8.0, andgithub.com/google/uuid
v1.1.2.)Schema and queries:
Demo code, assuming that:
queries
is an instance of the generatedQueries
type; andnu.nu
andnj.nj
columns contain at least one null row (for cases B and D).How I worked around this issue
I implemented two new types,
NullRawMessage
andNullUUID
, similar to Go's standard nullable SQL types, e.g.,sql.NullString
. It's available as a Go module:github.com/soroushj/sqlt
.sqlc can be configured to use these types:
This solves the issue:
How sqlc can fix this issue
There are a few ways to address this issue.
A new module. As mentioned before, the module I open-sourced fixes the issue. But for security reasons, probably it's not a good idea for sqlc to generate code that imports a module maintained by a random contributor. You can fork/copy this module and change the default Go types.
Generate required types. Another option for sqlc is to generate an additional Go file that contains the implementation of the required types. No new import is required in this case.
I prefer the first option: The module can be maintained independently. For clients, a bug fix is a version bump, not a re-generate.
Also, there might be other ways to fix this issue, but I like the consistency of
NullRawMessage
andNullUUID
with other nullable types.The text was updated successfully, but these errors were encountered: