Skip to content

fix(db2): use DEFAULT for auto-increment columns in inserts - #18422

Draft
wikirik-agent wants to merge 1 commit into
mainfrom
wikirik-agent/db2-create-empty-values
Draft

wikirik-agent wants to merge 1 commit into
mainfrom
wikirik-agent/db2-create-empty-values

Conversation

@wikirik-agent

Copy link
Copy Markdown
Contributor

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description of Changes

On Db2, Model.create({}) failed when the only value to insert was the auto-increment primary key, e.g. a model without timestamps whose other columns are nullable:

SELECT * FROM FINAL TABLE (INSERT INTO "Events");
-- SQL0104N  An unexpected token "FROM FINAL TABLE (INSERT INTO" was found following "SELECT * "

Db2 disabled supports.autoIncrement.defaultValue, so core dropped the null auto-increment column, and Db2 is the only dialect that supports neither DEFAULT VALUES nor VALUES (), which left a bare INSERT INTO.

bulkCreate([{}, {}]) had its own problems. The Db2 bulkInsertQuery override inserted ids from a counter kept on the query generator (INSERT INTO "Events" VALUES (1),(2)):

  • those ids bypass the identity sequence, so later inserts can collide with them;
  • it failed with SQL0117N when the table had other columns;
  • with returning: true it returned null ids.

Db2 accepts DEFAULT for identity columns, also in multi-row inserts and inside FINAL TABLE. DEFAULT VALUES, VALUES () and a bare VALUES (DEFAULT) on a table with several columns are rejected. So this PR:

  • stops disabling supports.autoIncrement.defaultValue and enables supports.bulkDefault for Db2, so inserts use ("id") VALUES (DEFAULT) like PostgreSQL;
  • replaces Db2's bulkInsertQuery with the shared implementation, wrapped in SELECT * FROM FINAL TABLE (...) when returning is set, as the IBM i dialect already does. Bulk values are now also escaped with their attribute type.

Replacing the override also means Db2 will get bulk-insert bind parameters from the shared implementation once #17752 lands, which covers the Db2 part of #18346. The two PRs touch the same Db2 method, so whichever merges second needs a small rebase.

Not changed: a Db2 model without any primary key still can't create({}), because there is no column to set to DEFAULT.

Tests

  • Integration, for all dialects: create({}) and bulkCreate([{}, {}]) on a model whose values all come from defaults. On main they fail on Db2 with SQL0104N and SQL0117N.
  • The Db2-only Worker.create({ id: 1 }) workarounds in belongs-to-many.test.js are removed, and Db2 now shares the PostgreSQL SQL expectation in bulk-create.test.js.
  • Unit: per-dialect SQL for an auto-increment column set to null in insertQuery, and for default-only rows in bulkInsertQuery with and without returning. The old Db2 expectation VALUES (0),(NULL) in sql/insert.test.js is now VALUES (0),(DEFAULT); the NULL version fails on the server with SQL0407N.
  • model/create, model/bulk-create and associations/belongs-to-many pass on db2, postgres, mysql, mariadb, mssql, sqlite3 and oracle; the Db2 and IBM i unit suites pass.

List of Breaking Changes

None.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

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.

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.

1 participant