Closed polderudo closed 1 year ago
The Null*** fields are part of the sql package so I'm not sure it falls under the purview of sqlc anyway. But the simple workaround is to make those types part of the Marshaller interface yourself. Eg:
func (x sql.NullString) Marshall() ([]byte, error) {
if !x.Valid {
return null, nil
}
return []byte(x.String), nil
}
I have not tested it by any means :) but I'm guessing that's the way to go as probably the people behind sql package won't want to get too opinionated on how people would want to encode null fields.
Hello Andrei, yes, thats how we use it right now:
func (f *NullTestStatus) MarshalJSON() ([]byte, error) {
if f.Valid {
return json.Marshal(f.TestStatus)
}
return json.Marshal(nil)
}
But of course would be nicer to have that automatictly generated.
Version
1.22.0
What happened?
Using postgres and "pgx/v5" is there any way to specify that any Nullxxx types should json.Marshal to null or the coresponding underlying type?
Doing a left join on the provided example, the status can be null, therefore we get a NullTestStatus type. Question is, if it's possible to specify, that this should be either null or the string itself on marshaling (without using emit_pointers_for_null_types=true of course).
Relevant log output
Database schema
SQL queries
Configuration
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