drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23k stars 550 forks source link

[BUG]: unexpected results using bun:sqlite #2273

Closed rev4324 closed 5 hours ago

rev4324 commented 4 months ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

No response

Describe the Bug

The same query yields different results using bun:sqlite compared to better-sqlite3 on Node. The bun:sqlite result is wrong - it has mismatched values and keys. The query uses two left joins.

Reproduction: https://github.com/rev4324/drizzle-repro

Expected behavior

Both drivers should yield the same results.

Environment & setup

I'm using macOS 14.4.1 (23E224) ARM64.

Jarred-Sumner commented 4 months ago

I suspect this is a bug in Bun. Can you file an issue in https://github.com/oven-sh/bun?

rev4324 commented 3 months ago

I don't think this is a bug in Bun, since the problem is caused by joined table column names overriding the original table column names. JS objects can't have the same key twice so the behavior is the same in bun:sqlite and better-sqlite3 when used directly, without Drizzle - the columns are overridden. Drizzle seems to mitigate this behavior in better-sqlite3 driver, but it doesn't do that in bun:sqlite one.

colecrouter commented 3 months ago

Can confirm it's not just Bun; I'm running into the same error with drizzle-orm/d1.

Edit:

Seems to be part of this file.

        this.logger.logQuery(this.query.sql, params);
-       const row = this.stmt.values(...params)[0];
+       const row = stmt.raw().get(...params) as unknown[];

As per docs:

.raw() Causes the prepared statement to return rows as arrays instead of objects. This is primarily used as a performance optimization when retrieving a very high number of rows. Column names can be recovered by using the .columns() method.

Not sure if this is it, but it sounds like what's being described.

Edit 2: Looks like I found the source of my problem...

L-Mario564 commented 7 hours ago

@rev4324 Hi there, is this still an issue? I cloned your reproduction repo and it seems both drivers now return the same values.

rev4324 commented 5 hours ago

Seems like the issue was fixed. @L-Mario564 is right, it's working now. I checked on Bun 1.1.26 and the results are exactly the same between bun:sqlite and better-sqlite3. The issue was likely due to Bun's sqlite driver itself though, since when I use Bun 1.1.8 (the version I did use when creating this issue), the issue is still there. It must have been fixed sometime between 1.1.8 and 1.1.26. Note: I used the same, old drizzle version with both Bun versions since I installed the deps with a frozen lockfile.