You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Question] Nullxxxx fields with "pgx/v5" and json.Marshal on left join #2817
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
Will output:
Select:
{"id":1,"status":"none"}
Select via left join:
[{"field1":11,"status":{"test_status":"none","valid":true}},{"field1":22,"status":{"test_status":"new","valid":true}},{"field1":33,"status":{"test_status":"","valid":false}}]
Select via join:
[{"field1":11,"status":"none"},{"field1":22,"status":"new"}]
Database schema
createtypetest_statusas enum('none', 'new');
CREATETABLEtest1
(
id intnot null,
status test_status not null default 'none'
);
CREATETABLEtest2
(
field1 intnot null,
test1_status_id intnull
);
insert into test1 values(1, 'none');
insert into test1 values(2, 'new');
insert into test2 values(11, 1);
insert into test2 values(22, 2);
insert into test2 values(33, null);
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:
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.
Activity
andrei-dascalu commentedon Oct 9, 2023
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:
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.
polderudo commentedon Oct 10, 2023
Hello Andrei, yes, thats how we use it right now:
But of course would be nicer to have that automatictly generated.