Skip to content

fix(sqlite): alias remapped select columns for D1 batch object rows - #6040

Open
arimu1 wants to merge 1 commit into
drizzle-team:mainfrom
arimu1:fix/6038-d1-batch-duplicate-column-aliases
Open

fix(sqlite): alias remapped select columns for D1 batch object rows#6040
arimu1 wants to merge 1 commit into
drizzle-team:mainfrom
arimu1:fix/6038-d1-batch-duplicate-column-aliases

Conversation

@arimu1

@arimu1 arimu1 commented Jul 20, 2026

Copy link
Copy Markdown

Summary

Fixes #6038

db.batch() on Cloudflare D1 returns object rows (client converts ROWS_AND_COLUMNS → objects). When a join select remaps two columns that share the same SQL name (e.g. users.id + posts.id as userId/postId), those keys collapse and d1ToRawMapping shifts values — while the identical query via .all() / .raw() stays correct.

Sqlite buildSelection now emits distinct AS aliases from the selection path (or table_column for multi-table selects that keep the raw column name), so object-row drivers keep one key per selected field. mapResultRow still maps by ordinal position.

Test plan

  • vitest run tests/sqlite-selection-aliases.test.ts — asserts generated SQL includes "users"."id" as "userId", "posts"."id" as "postId", "posts"."title" as "postTitle"
  • Repro from https://github.com/stinbox/drizzle-d1-batch-bug-repro against this branch (needs D1/wrangler)

AI disclosure

Prepared with Claude Code / Cursor assistance. I reviewed the D1 batch → object-row path and the sqlite selection builder, and verified the new unit test locally.

Made with Cursor

D1 batch collapses ROWS_AND_COLUMNS into objects, so duplicate SQL
column names (e.g. users.id + posts.id) lose values before mapResultRow.
Emit distinct AS aliases from the selection path so object keys stay unique.

Fixes drizzle-team#6038
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]: db.batch() shifts column values for duplicate column names on Cloudflare D1 (a single query maps them correctly)

1 participant