-
Notifications
You must be signed in to change notification settings - Fork 803
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
json.RawMessage does not support NULL #129
Comments
I believe we can generated the following type to fix this issue // NullRawMessage represents a json.RawMessage that may be null.
// NullRawMessage implements the Scanner interface so
// it can be used as a scan destination, similar to NullString.
type NullRawMessage struct {
RawMessage json.RawMessage
Valid bool // Valid is true if JSON is not NULL
}
// Scan implements the Scanner interface.
func (n *NullRawMessage) Scan(value interface{}) error {
n.RawMessage = json.RawMessage{}
if value == nil {
n.Valid = false
return nil
}
n.Valid = true
return n.RawMessage.Scan(value)
}
// Value implements the driver Valuer interface.
func (n NullRawMessage) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.RawMessage.Value()
} |
It appears that
|
We ran into this issue earlier, but this isn't really anything wrong with sqlc. We just added a null check in our code and if it was nil, we put in an empty array.
Definitely not ideal, but like you said, no Scanner. |
But if you find a fix, I'd love to see it! I'll keep an eye out as well. |
@mrcampbell,@kyleconroy, you can do something like this: func (n *NullRawMessage) Scan(value interface{}) error {
if value == nil {
n.RawMessage, n.Valid = json.RawMessage{}, false
return nil
}
buf, ok := value.([]byte)
if !ok {
return errors.New("canot parse to bytes")
}
n.RawMessage, n.Valid = buf, true
return nil
} |
Not sure if it is the best way, but I was able to work around the issue by adding a
|
Hi! Is someone working on this? I'm getting the same error when reading a jsonb value that can be null. |
Would also love to see a fix for this. I'm having this issue when reading json data from postgres. |
There is the same problem on mysql. |
@koolay I solved this for mysql using overrides in sqlc.yaml:
Note that the documentation is not up to date - |
Hey, @idan-stripe. I tried your fix but I still seem to be getting the same error as I was before. Have you changed anything else other than Edit: I'm using PostgreSQL |
Bug still not resolved in 08/13/2023. |
I did try the |
The text was updated successfully, but these errors were encountered: