Maximum call stack size exceeded: orderSelectedFields

I'm migrating from Prisma, and trying to replace an aggregate query. I'm getting an error with the following query, which is—as far as I can tell—right from the documentation on aggregate queries: https://orm.drizzle.team/docs/crud#aggregations
await db
.select({
total: sql<number>`sum(${t.tokenUse.total})`,
})
.from(t.tokenUse)
.where(
and(
eq(t.tokenUse.userId, user.id),
gt(t.tokenUse.createdAt, startOfToday),
eq(t.tokenUse.userTriggered, 1),
),
)
.groupBy(t.tokenUse.userId)
await db
.select({
total: sql<number>`sum(${t.tokenUse.total})`,
})
.from(t.tokenUse)
.where(
and(
eq(t.tokenUse.userId, user.id),
gt(t.tokenUse.createdAt, startOfToday),
eq(t.tokenUse.userTriggered, 1),
),
)
.groupBy(t.tokenUse.userId)
The error looks like an infinite loop—
@muse/nextjs:dev: RangeError: Maximum call stack size exceeded
@muse/nextjs:dev: at Array.values (<anonymous>)
@muse/nextjs:dev: at ./node_modules/drizzle-orm/alias-cf8e03cd.mjs:218:42
@muse/nextjs:dev: at Array.reduce (<anonymous>)
@muse/nextjs:dev: at orderSelectedFields (./node_modules/drizzle-orm/alias-cf8e03cd.mjs:218:35)
@muse/nextjs:dev: RangeError: Maximum call stack size exceeded
@muse/nextjs:dev: at Array.values (<anonymous>)
@muse/nextjs:dev: at ./node_modules/drizzle-orm/alias-cf8e03cd.mjs:218:42
@muse/nextjs:dev: at Array.reduce (<anonymous>)
@muse/nextjs:dev: at orderSelectedFields (./node_modules/drizzle-orm/alias-cf8e03cd.mjs:218:35)
Using the most recent version of Drizzle (0.28.6) and the Planetscale adapter. Any thoughts on what's going on?
6 Replies
Graham
GrahamOP16mo ago
Figured the issue out, and it was strange. I have a monorepo with multiple projects, and I was exporting Drizzle helpers from my packages/db repo like so: export * from "drizzle-orm"; I guess that was breaking the sql helper class somehow. Changing the export to this—
import * as drizzle from "drizzle-orm";
export { drizzle };
import * as drizzle from "drizzle-orm";
export { drizzle };
And then calling drizzle.sql'...' seems to have fixed it.
Angelelz
Angelelz16mo ago
How were you importing it where you were using it? This is interesting
Graham
GrahamOP16mo ago
import { sql, eq, inArray, ... } from "@project/db" Which now reads import { drizzle } from "@project/db"
Angelelz
Angelelz16mo ago
The only explanation I can come up with is that you're importing sql twice Drizzle support cjs and mjs and have both mjs and cjs exports Maybe the import * was bringing in both
Graham
GrahamOP16mo ago
Oh interesting, maybe so! When I was debugging I tried running sql"blahblah".getSQL() and got an error that getSQL didn't exist on the tagged template function. Would that happen if I was using cjs in an mjs environment or vice versa? (not sure which I'm in)
Braveheart
Braveheart12mo ago
I just got this today for a simple insert
Want results from more Discord servers?
Add your server