sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
12.32k stars 779 forks source link

incorrect field type generated for nullable column #3025

Open jusongchen opened 9 months ago

jusongchen commented 9 months ago

Version

1.24.0

What happened?

Given this SQL on mysql

-- name: TestSQLC :many
select NULL name, 12  age
union all 
select 'name', 13

sqlc (version 1.18 and later) generates this data type

type TestSQLCRow struct {
    Name string `json:"name"`
    Age  int32  `json:"age"`
}

This results in error 'converting NULL to string is unsupported' as a NULL value on a db row cannot be stored to a string field.

The same query works on the sqlc version 1.17.2 as it generates this struct:

type TestSQLCRow struct {
    Name interface{} `json:"name"`
    Age  interface{} `json:"age"`
}

Relevant log output

No response

Database schema

None

SQL queries

-- name: TestSQLC :many
select NULL name, 12  age
union all 
select 'name', 13

Configuration

{
    "version": "1",
    "packages": [
        {
            "name": "db",
            "path": "./db",
            "queries": "./",
            "schema": "./",
            "engine": "mysql",
            "emit_prepared_queries": true,
            "emit_interface": false,
            "emit_exact_table_names": false,
            "emit_empty_slices": false,
            "emit_exported_queries": false,
            "emit_json_tags": true,
            "json_tags_case_style": "camel",
            "output_db_file_name": "db.go",
            "output_models_file_name": "models.go",
            "output_querier_file_name": "querier.go"
        }
    ]
}

Playground URL

https://play.sqlc.dev/p/1319e7502b91a9af5a5552b4cfc3d37284d047b40121f477dc05c5ec427b4ceb

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

andrewmbenton commented 9 months ago

The correct type for the Name field would be sql.NullString. With PostgreSQL and the database-backed analyzer turned on that is the result. I would expect a forthcoming MySQL database-backed analyzer will resolve this.

fasolens commented 8 months ago

I have the same. interface{} is generated instead of sql.NullString. Do you plan to add support for pointers for nullable parameters in MySQL engine?