Skip to content

Add onConflictDoNothing and onConflictDoUpdate to inserts - #11

Merged
Makisuo merged 1 commit into
feat/insert-returningfrom
feat/insert-on-conflict
Oct 4, 2026
Merged

Makisuo merged 1 commit into
feat/insert-returningfrom
feat/insert-on-conflict

Conversation

@Makisuo

@Makisuo Makisuo commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Phase 3 of design/writes.md. Stacked on #10, which is stacked on #9.

CH.insertInto(Counters)
	.values({ key: "a", count: 1 })
	.onConflictDoUpdate({
		target: ["key"],
		set: ($, excluded) => ({ count: $.count.add(excluded.count) }),
		where: ($) => $.locked.eq(false),
	})
	.returning("key", "count")
INSERT INTO "counters" ("key", "count")
VALUES ($1, $2)
ON CONFLICT ("key") DO UPDATE SET "count" = "counters"."count" + "excluded"."count" WHERE "counters"."locked" = FALSE
RETURNING "key" AS "key", "count" AS "count"

What changes

  • Drizzle's shape. The options match Drizzle's (target, targetWhere, set, where), so Maple's 68 ON CONFLICT sites move over with renames. This replaces the chained form sketched in the design note; the note now records the change.
  • Targets. target is column names or { constraint }. targetWhere gives a partial index's predicate. onConflictDoUpdate requires a target, enforced in the types.
  • set is a record of values, params or expressions, or a callback over $ (the existing row) and excluded. $ is qualified with the table name, because an unqualified column there is ambiguous with excluded.
  • Tenant scope. A set that writes the tenant column counts toward tenant scope.
  • Dialects. New optional DialectClauses.onConflict. ClickHouse refuses with a QueryBuilderDefect.
  • Exports. ConflictTarget, ConflictSet, OnConflictDoNothing and OnConflictDoUpdate are exported for helpers.

Testing

  • bun run typecheck and bun run test: 502 unit tests and the doc checks pass.
  • Unit tests cover every target form, set/where SQL and params, tenant scope through set, and the failures and defects.
  • New PGlite test: a real upsert that adds to the existing count, is skipped by where on a locked row (returns nothing), and DO NOTHING returning only the row that was inserted.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

ON CONFLICT on Postgres, with Drizzle's options so call sites move over
with renames: target (columns or a named constraint), targetWhere for a
partial index, set as a record or a callback over the existing row and
excluded, and where to limit the update. The existing row is qualified
with the table name, since an unqualified column is ambiguous with
excluded. A SET that writes the tenant column counts toward tenant
scope.

DialectClauses.onConflict is optional; ClickHouse refuses at compile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 12 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7613f9f8-baff-4132-808c-9d4608cca0bc
📥 Commits

Reviewing files that changed from the base of the PR and between ee97ecb and eab33d4.

📒 Files selected for processing (12)
  • CHANGELOG.md
  • design/writes.md
  • docs/inserts.md
  • docs/reference.md
  • src/ch/compile.ts
  • src/ch/dialect.ts
  • src/ch/index.ts
  • src/ch/insert.test-d.ts
  • src/ch/insert.test.ts
  • src/ch/insert.ts
  • src/database/database.test.ts
  • src/pg/dialect.ts
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@Makisuo
Makisuo added this pull request to stack #13 October 3, 2026 23:07
@Makisuo
Makisuo merged commit 70f674a into main Oct 4, 2026
4 checks passed
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