Skip to content

Insert type params are not be nullable if the column is nullable #254

@Nydauron

Description

@Nydauron

In the insertion test cases, the parameter types always evaluate without a null union even if the column is nullable. The expected behavior is that parameters that are representing nullable columns should have the type T | null.

Reproducible Example

An example of this can be found within the test cases:

run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None, None),
//// TS query ////
r#"
import { sql } from "sqlx-ts";
const someInputQuery = sql`
INSERT INTO items (id, name, rarity, flavor_text)
VALUES
(?, ?, 'epic', 'test'),
(1, 'test', ?, ?);
`
"#,
//// Generated TS interfaces ////
r#"
export type SomeInputQueryParams = [[number, string], [string, string]];

The schema of the table items is as follows:

mysql> describe items;
+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int          | NO   | PRI | NULL    | auto_increment |
| name         | varchar(100) | NO   |     | NULL    |                |
| rarity       | varchar(50)  | YES  |     | NULL    |                |
| inventory_id | int          | YES  | MUL | NULL    |                |
| flavor_text  | text         | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

The parameter type generated for this query is [[number, string], [string, string]]. It should instead be [[number, string], [string | null, string | null]] since rarity and flavor_text are nullable columns.

Running an insert query manually with rarity and flavor_text set to NULL runs without errors and is present within the table upon read:

mysql> INSERT INTO items (id, name, rarity, flavor_text) VALUES (2, 'awesome', 'epic', 'test'), (1, 'test', NULL, NULL);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from items;
+----+---------+--------+--------------+-------------+
| id | name    | rarity | inventory_id | flavor_text |
+----+---------+--------+--------------+-------------+
|  1 | test    | NULL   |         NULL | NULL        |
|  2 | awesome | epic   |         NULL | test        |
+----+---------+--------+--------------+-------------+
2 rows in set (0.00 sec)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions