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
sqlc.narg is a great addition. Unfortunately, it does not seem to work for enum types. I would really like to be able to use nullable enums, as empty strings are invalid enums and throw an error.
In the example provided, it would be great if the method definition used *Colors to make it nullable:
CREATETYPEcolorsAS ENUM (
'red',
'blue',
'green'
);
-- Example queries for sqlcCREATETABLEboxs (
id BIGSERIALPRIMARY KEY,
name textNOT NULL,
color colors
);
SQL queries
-- name: GetBoxes :manySELECT*FROM boxs
WHERE
CASE
WHEN sqlc.narg('color') :: colors !=null THEN
color =sqlc.narg('color')
ELSE true
END;
The thing about this behavior you encountered is that there is no way to handle null values for the Colors enum. There is sql.NullString, sql.NullTime, sql.NullBool, but there isn't a NullColors type that can handle null values. Later, I will link a repo showing how to use enums that can be nullable.
Notice that you have to manually create a type to handle your enums and a second type that implements both sql.Scanner and driver.Valuer to handle your enums when they can be nullable.
Another thing that happens is that sqlc generates a type for your enum regardless of any type that you might provide to it. Maybe I can work a PR so that sqlc might generate the nullable type on its own.
@danielbprice well, I did'nt know. I saw the problem and tried to fix it. The other two proposals seem to have more work done than mine. And seeing that they have proper functioning code already, I will just wait for when a new version with this feature is released.
Activity
eullerpereira94 commentedon Jun 24, 2022
The thing about this behavior you encountered is that there is no way to handle null values for the Colors enum. There is sql.NullString, sql.NullTime, sql.NullBool, but there isn't a NullColors type that can handle null values. Later, I will link a repo showing how to use enums that can be nullable.
Emyrk commentedon Jun 24, 2022
Would like to see that example!
eullerpereira94 commentedon Jun 24, 2022
This repository shows to how to do it: https://github.com/eullerpereira94/sqlc_enum_test
Notice that you have to manually create a type to handle your enums and a second type that implements both
sql.Scanneranddriver.Valuerto handle your enums when they can be nullable.Another thing that happens is that sqlc generates a type for your enum regardless of any type that you might provide to it. Maybe I can work a PR so that sqlc might generate the nullable type on its own.
Emyrk commentedon Jun 24, 2022
@eullerpereira94 Thanks for the example. I will take a look and replicate in my repo if it's better than my current method.
It would be great to autogen it as all enums should be primitive types under the hood!
danielbprice commentedon Jun 28, 2022
There are two open MRs for this, see #1582 and #1485
eullerpereira94 commentedon Jun 28, 2022
@danielbprice well, I did'nt know. I saw the problem and tried to fix it. The other two proposals seem to have more work done than mine. And seeing that they have proper functioning code already, I will just wait for when a new version with this feature is released.
sbres commentedon Jul 5, 2022
I'm waiting to get the PR merged as well, I'm starting to get worried it won't get merged anytime soon
sbres commentedon Jul 5, 2022
Never mind it just got merged 🎉
kyleconroy commentedon Aug 29, 2022
Confirmed that this now works :)
https://play.sqlc.dev/p/0ac3182108229aa90ff756658d45e2fc43c865517fa63f42c4339cb2b462a11e