Skip to content

NULL JSON field fails to Scan into json.RawMessage #3373

Open
@Jille

Description

@Jille
Contributor

Version

1.26.0

What happened?

I have a MariaDB table with a nullable JSON column but selecting it when the value is NULL fails to Scan() it into the field.

I'm using the github.com/go-sql-driver/mysql driver.

Relevant log output

sql: Scan error on column index 1, name "my_json_column": unsupported Scan, storing driver.Value type <nil> into type *json.RawMessage


### Database schema

```sql
CREATE TABLE authors (
  id   INT PRIMARY KEY,
  bio  JSON
);

SQL queries

-- name: GetAuthor :one
SELECT * FROM authors WHERE id = ?;

Configuration

No response

Playground URL

https://play.sqlc.dev/p/8e1085762b38b90fb51da2bdac5fe4d2b37d6b89cfae5562063e295d3a829a18

What operating system are you using?

Linux

What database engines are you using?

MySQL

What type of code are you generating?

Go

Activity

added
bugSomething isn't working
triageNew issues that hasn't been reviewed
on May 10, 2024
changed the title [-]NULL JOSN [/-] [+]NULL JSON field fails to Scan into json.RawMessage[/+] on May 10, 2024
removed
triageNew issues that hasn't been reviewed
on Aug 5, 2024
jarri-abidi

jarri-abidi commented on Sep 2, 2024

@jarri-abidi

any plans to fix this @kyleconroy? if you can suggest how to do it, maybe i can try

advait

advait commented on Jan 7, 2025

@advait

The bug actually appears here where we fail to check for notNull in json fields.

In my testing, it seems if we output *.json.RawMessage for nullable fields, the Scan operation succeeds successfully for null values.

I don't see any other references of pointers inside of sqlc-generated code (vs. explicit NullRawMessage types as exist in the postgres implementation) so I'm uncertain pointers would be acceptable. In any case I'll spin up a PR shortly for discussion.

linked a pull request that will close this issue on Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @kyleconroy@Jille@advait@jarri-abidi

      Issue actions

        NULL JSON field fails to Scan into json.RawMessage · Issue #3373 · sqlc-dev/sqlc