"Prepared statement already exists" error

I'm experiencing a problem with Drizzle ORM and Vercel’s Postgres (which I believe is Neon Postgres under the hood) in my SvelteKit application deployed on Vercel. Everything functioned perfectly until I tried to transition my request handling to edge functions using prepared statements. Here’s what I've implemented: 1. Defined a prepared statement in a separate file:
export const getPublishedJokesPrep = db
.select()
.from(jokesTable)
.where(eq(jokesTable.isPublished, true))
.orderBy(desc(jokesTable.votes))
.prepare('getPublishedJokesPrep');
export const getPublishedJokesPrep = db
.select()
.from(jokesTable)
.where(eq(jokesTable.isPublished, true))
.orderBy(desc(jokesTable.votes))
.prepare('getPublishedJokesPrep');
2. Executed the prepared statement in the request handler: getPublishedJokesPrep.execute(); The first execution works fine, but subsequent ones trigger a “prepared statement already exists” error. It seems each request to the handler tries to recreate the prepared statement. This issue seems similar to what others have encountered (https://discord.com/channels/1043890932593987624/1150720833199808584, https://discord.com/channels/1043890932593987624/1124136214086299690), but there are no solutions in these threads. Has anyone faced and resolved this? Any insights into the root cause or potential solutions would be greatly appreciated.
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
7 Replies
Hocus
Hocus12mo ago
did you figure it out? I just ran into the same issue
Monkie
Monkie12mo ago
I don't know much about this so just throwing ideas out there lol. are you using a pooled connection ? And if so is the url you setting correct for pooled connection
Hocus
Hocus12mo ago
I tried all three urls provided by vercel: POSTGRES_URL POSTGRES_PRISMA_URL POSTGRES_URL_NON_POOLING all give the same error. Not sure though if I should have reset the db somehow or clear the existing prepared statements before switching URLs. I did find very little information on this specially for drizzle. I see that this is mentioned in the docs for prisma, but yeah not sure what else to try
Monkie
Monkie12mo ago
You wanna be using the postgres url and postgres url non pooling ones show me how you uave the client set up ie code let me have a look
Hocus
Hocus12mo ago
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";

import * as schema from "./schema";

export const db = drizzle(sql, { schema });
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";

import * as schema from "./schema";

export const db = drizzle(sql, { schema });
not much to look at I am afraid 😄
Hocus
Hocus12mo ago
No description
Hocus
Hocus12mo ago
and these are the env variables that vercel added automatically when I linked the project to vercel storage
Want results from more Discord servers?
Add your server