Skip to content

fix: stop dialects from dropping DEFAULT values their server accepts - #18427

Merged
WikiRik merged 5 commits into
wikirik-agent/attributes-to-sql-testsfrom
wikirik-agent/attributes-to-sql-defaults
Sep 19, 2026
Merged

WikiRik merged 5 commits into
wikirik-agent/attributes-to-sql-testsfrom
wikirik-agent/attributes-to-sql-defaults

Conversation

@wikirik-agent

@wikirik-agent wikirik-agent commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Description of Changes

Second of the stack re-implementing #17121, stacked on #18399.

#18399 pinned current behaviour exactly. This is the first of three PRs that change it. Every expectation that moves in attribute-to-sql.test.ts is a behaviour delta, so the test diff is the review surface.

Seven of the nine dialects carried a list of types that "cannot have a default", or an equivalent inline guard. None of them worked. Once they were made to work it turned out most were describing a restriction the server does not have, so the net effect of this PR is that a defaultValue the user asked for is no longer silently discarded.

fix: make the "this type cannot have a default" guards work again

Every such guard was checking a condition that can never be true on v7 data types:

  • attribute.type._binary was a v6 property of STRING/CHAR set by .BINARY. It does not exist in v7 — I checked the v6 source in git history to confirm what it meant. It is now attribute.type.options?.binary.
  • db2, mssql and oracle compared attribute.type !== 'TEXT', i.e. a DataType instance against a string.
  • mariadb, mysql, snowflake and ibmi looked the rendered SQL string up in a set of type names, so DataTypes.TEXT('tiny') → TINYTEXT missed, and ibmi never matched at all since its BLOB renders as BLOB(1M).

Lookups now use a new attributeTypeToDataTypeId core utility built on the existing getDataTypeId(), which returns a type's dialect-independent identity. Each dialect keeps exactly the list of types it already named — this commit does not re-decide which types reject defaults. The next four do.

fix: stop dropping defaults on LOB and binary string columns

Verified against Db2 LUW 12.1.4, SQL Server 2025, Oracle 23.26 Free, MySQL 8.4 and MariaDB 11.6:

  • db2, mssql and oracle all accept a DEFAULT on their LOB types. CLOB DEFAULT 'abc', NVARCHAR(MAX) DEFAULT N'abc' and BLOB DEFAULT BLOB('abc') all work, and the value round-trips. The SQL Server folklore appears to come from CREATE TABLE's rule that CHECK constraints cannot be defined on text/ntext/image — a different clause. Those three lists are removed.
  • .BINARY on a string type is a collation, not a storage class, and every server tested accepts a default on such a column. That guard is removed everywhere rather than repaired.

fix(mariadb,mysql): keep the DEFAULT on BLOB, TEXT, GEOMETRY and JSON columns

MySQL accepts a default on those types as long as it is written as an expression, i.e. wrapped in parentheses, since 8.0.13. The minimum supported version is 8.0.20, so every supported server can take one. The drop becomes a parenthesisation:

-- before:  `json` JSON
-- after:   `json` JSON DEFAULT (CAST('null' AS JSON))

Worth noting the guard sat before the existing BaseSqlExpression parenthesisation, so the MySQL 8.0.13 expression-default support already in the codebase was unreachable for exactly the types that needed it.

MariaDB has accepted defaults on BLOB/TEXT since 10.2.1 (MDEV-10134) and the minimum supported version is 10.4.30, so its list is removed outright. mariadb also emitted expression defaults unparenthesised, which MariaDB rejects for anything that is not a bare function call — DEFAULT 1+1 fails, DEFAULT (1+1) works. Those are now parenthesised as they already were on mysql, which is why three create-table expectations change.

Verified end to end on MySQL 8.4 and MariaDB 11.6: the columns are created and the values round-trip.

fix(ibmi,snowflake): remove the last two typeWithoutDefault lists

Both are verbatim copies of mysql's, made in the commits that introduced each dialect, and neither has ever fired.

ibmi's list is removed. Db2 for i documents a closed list of types that cannot take a DEFAULT — ROWID, identity, row change timestamp, row-begin, row-end, transaction-start-ID and generated expression columns — and LOBs are not in it. It goes further and names BLOB and CLOB among the types that may use the cast-function default form, which is exactly what escapeBuffer already produces. The data type is now passed to escape, without which a Buffer default throws, and that in turn makes the manual true/false → 1/0 coercion unnecessary: the BOOLEAN type serialises itself once it is known.

snowflake keeps only BLOB, for a different reason than the old comment gave. TEXT is a documented synonym of VARCHAR and Snowflake's DEFAULT grammar has no type predicate, so dropping a default there was plain data loss. JSON is not a Snowflake type name at all. BLOB stays because Snowflake has no BLOB type either — its binary types are BINARY and VARBINARY, and binary literals are written TO_BINARY(..., 'HEX') rather than X'...' — so that column cannot be created regardless of its default. That needs its own fix, noted with a TODO.

Both of these conclusions are documentation-based. There is no Snowflake account and no Db2 for i server available here.

fix(mariadb,mysql,oracle): tell escape what type the default belongs to

escape guesses the data type from the value when it is not told one. An array default on a JSON column is guessed as ARRAY, which none of these three support, so generating the column threw. This was latent until the default started being emitted at all — mariadb and mysql used to drop defaults on JSON columns, so escape was never reached. Caught by the DataTypes.JSON integration test on mariadb and mysql.

Verification

All nine dialect unit suites pass at every commit in this PR, with clean eslint, clean prettier, and clean tsc on both --noEmit and test/tsconfig.json. Integration suites for the stack as a whole run against real servers: mssql 411 passing, oracle 388 passing (needs TZ=UTC), db2 19 passing on the query-interface suite. ibmi and snowflake remain unverified — neither can be run locally.

List of Breaking Changes

Seven dialects now emit a DEFAULT clause for column types where they previously emitted none. A model that declared a defaultValue on a TEXT/BLOB/JSON/GEOMETRY column silently got no default before and will now get the one it asked for.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved schema generation for column default values across supported database dialects.
    • Defaults are now generated consistently for text, binary, JSON, geometry, and blob-related column types where supported.
    • Database expressions used as defaults now receive dialect-appropriate formatting, including required parentheses.
    • Corrected MariaDB, MySQL, Snowflake, DB2, IBM i, MSSQL, and Oracle default-value SQL output.
    • Preserved database-specific handling for unsupported binary defaults and JSON values.

@coderabbitai

coderabbitai Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 69e46dde-98a7-4887-b5e9-681f5b338c22

📥 Commits

Reviewing files that changed from the base of the PR and between 80ae785 and fbd4998.

📒 Files selected for processing (11)
  • packages/core/src/abstract-dialect/data-types-utils.ts
  • packages/core/src/abstract-dialect/query-generator.js
  • packages/core/test/unit/query-generator/attribute-to-sql.test.ts
  • packages/core/test/unit/query-interface/create-table.test.ts
  • packages/db2/src/query-generator.js
  • packages/ibmi/src/query-generator.js
  • packages/mariadb/src/query-generator.js
  • packages/mssql/src/query-generator.js
  • packages/mysql/src/query-generator.js
  • packages/oracle/src/query-generator.js
  • packages/snowflake/src/query-generator.js

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

Changes

Default generation

Layer / File(s) Summary
Type identity and SQL type mapping
packages/core/src/abstract-dialect/data-types-utils.ts, packages/core/src/abstract-dialect/query-generator.js, packages/*/src/query-generator.js
Adds attributeTypeToDataTypeId and removes dialect options from selected type-to-SQL calls.
Dialect default rendering
packages/db2/src/query-generator.js, packages/ibmi/src/query-generator.js, packages/mariadb/src/query-generator.js, packages/mssql/src/query-generator.js, packages/mysql/src/query-generator.js, packages/oracle/src/query-generator.js, packages/snowflake/src/query-generator.js
Dialect generators emit schemable defaults for additional types. MariaDB and MySQL add dialect-specific parentheses and type-aware escaping.
SQL generation expectations
packages/core/test/unit/query-generator/attribute-to-sql.test.ts, packages/core/test/unit/query-interface/create-table.test.ts
Updates expected SQL for date, text, binary, JSON, geometry, and expression defaults.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: wikirik

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 11 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving DEFAULT values that supported dialect servers accept.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

wikirik-agent and others added 5 commits September 19, 2026 09:49
Every dialect that refuses a DEFAULT for certain column types was checking
conditions that can never be true on v7 data types:

- `attribute.type._binary` was a v6 property of STRING/CHAR set by `.BINARY`.
  It does not exist in v7, so the check was always a no-op. It is now
  `attribute.type.options?.binary`, which is where that flag lives today.
- db2, mssql and oracle compared `attribute.type !== 'TEXT'`, comparing a
  DataType instance against a string, so it never matched.
- mariadb, mysql, snowflake and ibmi looked up the rendered SQL string in a set
  of type names. That only matched the default length variant, and never
  matched at all on ibmi, whose BLOB renders as `BLOB(1M)`.

The lookups now use `attributeTypeToDataTypeId`, added to the core data type
utilities, which returns the dialect-independent identity of a type. Each
dialect keeps exactly the list of types it already named.

As a result db2, mssql and oracle stop emitting a DEFAULT for BLOB and TEXT
columns, and ibmi stops emitting one for BLOB. A raw `'BLOB'` string type with
a default no longer throws while trying to escape the value.

Co-Authored-By: Claude Opus 5 <[email protected]>
Follow-up to the previous commit, which restored guards that turned out to be
describing restrictions that do not exist. Verified against db2 12.1.4, SQL
Server 2025, Oracle 23.26, MySQL 8.4 and MariaDB 11.6:

- db2, mssql and oracle accept a DEFAULT on their LOB types. `CLOB DEFAULT
  'abc'`, `NVARCHAR(MAX) DEFAULT N'abc'` and `BLOB DEFAULT BLOB('abc')` all
  work. The SQL Server folklore appears to come from CREATE TABLE's rule that
  CHECK constraints cannot be defined on text/ntext/image, which is a different
  clause. Those three lists are removed.
- `.BINARY` on a string type is a collation, not a storage class, and every
  server tested accepts a default on such a column. That guard is removed
  everywhere rather than repaired.

mariadb, mysql, snowflake and ibmi keep the lists they already had, now matched
by data type identity rather than by rendered SQL string.

Without this, a `defaultValue` the user asked for would silently vanish.

Co-Authored-By: Claude Opus 5 <[email protected]>
… columns

Both dialects emitted no DEFAULT at all for these four types.

MySQL accepts one as long as it is written as an expression, i.e. wrapped in
parentheses, since 8.0.13 — and the minimum supported version is 8.0.20. The
drop is now a parenthesisation, so the value reaches the server instead of
silently vanishing. Verified on MySQL 8.4.

MariaDB has accepted defaults on them since 10.2.1 and the minimum supported
version is 10.4.30, so the list is removed outright. mariadb also emitted
expression defaults unparenthesised, which MariaDB rejects for anything that is
not a bare function call, so those are now parenthesised as they already were on
mysql. Verified on MariaDB 11.6.

Co-Authored-By: Claude Opus 5 <[email protected]>
Both are verbatim copies of mysql's, made in the commits that introduced each
dialect, and neither has ever fired: they matched on the rendered SQL string,
which is `BLOB(1M)` on ibmi and never the bare `BLOB` the set contained.

ibmi's list is removed. Db2 for i documents a closed list of types that cannot
take a DEFAULT — ROWID, identity, row change timestamp, row-begin, row-end,
transaction-start-ID and generated expression columns — and LOBs are not in it.
It goes further and names BLOB and CLOB among the types that may use the
cast-function default form, which is exactly what `escapeBuffer` already
produces. The data type is now passed to `escape`, without which a Buffer
default throws, and that in turn makes the manual true/false to 1/0 coercion
unnecessary: the BOOLEAN type serializes itself once it is known.

snowflake keeps only BLOB, and for a different reason than the old comment
gave. TEXT is a documented synonym of VARCHAR and Snowflake's DEFAULT grammar
has no type predicate, so dropping a default there was plain data loss. JSON is
not a Snowflake type name at all. BLOB stays because Snowflake has no BLOB type
either — its binary types are BINARY and VARBINARY, and binary literals are
written TO_BINARY(..., 'HEX') rather than X'...' — so that column cannot be
created regardless of its default. That needs its own fix, noted with a TODO.

Both conclusions are documentation-based. Neither dialect can be tested here.

Co-Authored-By: Claude Opus 5 <[email protected]>
`escape` guesses the data type from the value when it is not told one. An array
default on a JSON column is guessed as ARRAY, which none of these three
dialects support, so generating the column threw.

This was latent until the default started being emitted at all: mariadb and
mysql used to drop defaults on JSON columns, so `escape` was never reached.
oracle was not failing but had the same hole, and every other dialect already
passed the type.

Caught by the DataTypes.JSON integration test on mariadb and mysql.

Co-Authored-By: Claude Opus 5 <[email protected]>
@WikiRik
WikiRik force-pushed the wikirik-agent/attributes-to-sql-defaults branch from fbd4998 to fe5fff4 Compare September 19, 2026 07:49
@WikiRik
WikiRik merged commit d094441 into main Sep 19, 2026
144 of 145 checks passed
@WikiRik
WikiRik deleted the wikirik-agent/attributes-to-sql-defaults branch September 19, 2026 10:15
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.

2 participants