Skip to content

[Mysql-kit] Fix introspection dropping a zero default on numeric columns#5997

Open
Otto-Deviant1904 wants to merge 1 commit into
drizzle-team:mainfrom
Otto-Deviant1904:fix-mysql-introspect-zero-default
Open

[Mysql-kit] Fix introspection dropping a zero default on numeric columns#5997
Otto-Deviant1904 wants to merge 1 commit into
drizzle-team:mainfrom
Otto-Deviant1904:fix-mysql-introspect-zero-default

Conversation

@Otto-Deviant1904

Copy link
Copy Markdown

Fixes #5911.

introspect-mysql.ts's column() function decides whether to append .default(...) to a generated column definition using a plain truthy check on the parsed default value: defaultValue ? '.default(...)' : ''.

That breaks for a legitimate default of 0 (numeric types) or false (boolean), since both are falsy in JS. int and tinyint already guard against this correctly with typeof defaultValue !== 'undefined', but smallint, mediumint, bigint, boolean, double, float, and real still used the truthy check, so a NOT NULL DEFAULT 0 column on any of those types silently loses its .default() during introspection — matching the report exactly (a MySQL FLOAT NOT NULL DEFAULT 1.5 column round-trips fine, but the same column with DEFAULT 0 doesn't).

Fixed by applying the same typeof defaultValue !== 'undefined' check int/tinyint already use, to the remaining affected types.

Verified against a real mysql:8.0 container:

  • Before the fix: float().notNull().default(0) introspects as float().notNull() — default silently dropped.
  • After the fix: introspects correctly as float().default(0).notNull().

Added two regression tests to tests/introspect/mysql.test.ts: one matching the reported float case, one covering the other affected types (smallint, mediumint, bigint, double, real) with a zero default. Ran the full introspect/mysql.test.ts suite — all 12 pass.

Left boolean out of the regression test even though I fixed that code path too — MySQL reports BOOLEAN columns as tinyint(1) in information_schema, not boolean, so that branch turned out to be unreachable via real introspection and hits an unrelated, pre-existing bug in the generated file's import list when exercised directly. Didn't want to fix an unrelated issue in this PR, but happy to file it separately if useful.

introspect-mysql.ts appends .default(...) based on a truthy check of
the parsed default value. int and tinyint already guard this correctly
with typeof defaultValue !== 'undefined', but smallint, mediumint,
bigint, boolean, double, float, and real still used the truthy check,
so a legitimate default of 0 (or false) was silently dropped during
introspection. Applied the same typeof check already used by int and
tinyint to the remaining affected types.

Fixes drizzle-team#5911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: mysql default is not recognized for float type

1 participant