Prisma $executeRaw`` function inside Next.js 13 route handlers throws 'Raw query failed. Code: 1105' Error

264 Views Asked by At

I have a a button that sends an api request to my Route Handler in Next.js 13. I am trying to delete a post from the database using a dynamic query built from parameters. However, it is throwing a weird error when I try to call this route. The code I am using:

api/post/route.ts:

export async function DELETE(req: Request) {
  const body: DeletePostProps = await req.json();
  const { table, postId } = body;
  console.log(table, postId);

  const session = await getServerSession(authOptions);

  if (session?.user) {
    await db.$executeRaw`
      DELETE FROM ${table} WHERE id = ${postId};
      `;
    return new Response(`Deleted post ${postId}`, {
      status: 200,
    });
  } else {
    return new Response('Unauthorized', {
      status: 401,
    });
  }
}

the error:

- error PrismaClientKnownRequestError: 
Invalid `prisma.$executeRaw()` invocation:


Raw query failed. Code: `1105`. Message: `unknown error: syntax error at position 21 near ':v1'`
    at vn.handleRequestError (/Users/polo/mysite/node_modules/@prisma/client/runtime/library.js:123:6730)
    at vn.handleAndLogRequestError (/Users/polo/mysite/node_modules/@prisma/client/runtime/library.js:123:6119)
    at vn.request (/Users/polo/mysite/node_modules/@prisma/client/runtime/library.js:123:5839)
    at async l (/Users/polo/mysite/node_modules/@prisma/client/runtime/library.js:128:9763)
    at async DELETE (webpack-internal:///(rsc)/./app/api/post/route.ts:41:9)
    at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:254:37) {
  code: 'P2010',
  clientVersion: '5.2.0',
  meta: {
    code: '1105',
    message: "unknown error: syntax error at position 21 near ':v1'"
  }
}
- error PrismaClientKnownRequestError: 
Invalid `prisma.post.findMany()` invocation:


Can't reach database server at `aws.connect.psdb.cloud`:`3306`

Please make sure your database server is running at `aws.connect.psdb.cloud`:`3306`.
    at async Projects (./app/projects/page.tsx:18:19)

The only thing I was able to come up with so far is disabling Next.js caching with this:

export const dynamic = 'force-dynamic';

without any results.

0

There are 0 best solutions below