drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: unexpected results using bun:sqlite

Open rev4324 opened this issue 1 year ago • 2 comments
trafficstars

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.

rev4324 avatar May 07 '24 18:05 rev4324

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

Jarred-Sumner avatar May 09 '24 03:05 Jarred-Sumner

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.

rev4324 avatar May 11 '24 12:05 rev4324

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...

colecrouter avatar May 28 '24 20:05 colecrouter

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

L-Mario564 avatar Sep 06 '24 17:09 L-Mario564

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.

rev4324 avatar Sep 06 '24 19:09 rev4324