This project demonstrates an issue with Prisma's TypedSQL feature in a Next.js application.
- Node.js v20.15.1 (i use this version)
- npm (10.8.3)
- PostgreSQL database (neon.tech Postgre 16)
-
Clone the repository:
git clone https://github.com/Gubiar/prisma-type-safe-sql.git cd prisma-type-safe-sql -
Install dependencies:
npm install -
Set up your environment variables: Create a
.envfile in the root directory and add your database URL:DATABASE_URL="postgresql://username:password@localhost:5432/your_database" -
Push the Prisma schema to your database:
npx prisma db push
-
Start the development server:
npm run dev -
Open http://localhost:3000 in your browser.
To reproduce the error with Prisma's TypedSQL feature:
-
Ensure you have a SQL file named
getPosts.sqlin theprisma/sql/directory with the following content:SELECT p.id, p.title, p.author, p.content, p."createDate", s.name AS "statusName" FROM "Post" p JOIN "Status" s ON p."statusId" = s.id
-
Run the following command:
npx prisma generate --sql -
You should see the following error:
Error: Errors while reading sql files: In prisma\sql\getPosts.sql: Error: ERROR: prepared statement "s2" does not exist
Troubleshooting (UPDATE prisma/prisma#25106 (reply in thread))
If you encounter the "prepared statement "s2" does not exist" error, try the following solution:
-
Add a
DIRECT_URLto your.envfile. This is particularly important if you're using a database service like Neon.DATABASE_URL="your_connection_string_here" DIRECT_URL="your_direct_connection_string_here"For more information on using a direct connection, especially with Neon, see: Neon's guide on using Prisma
-
Update prisma/schema.prisma
generator client { provider = "prisma-client-js" previewFeatures = ["typedSql"] } datasource db { provider = "postgresql" url = env("DATABASE_URL") directUrl = env("DIRECT_URL") } -
After adding the
DIRECT_URL, try runningnpx prisma generate --sqlagain.
- The SQL query works fine when executed directly in the database.
- The query also works when used with
prisma.$queryRaw. - This issue specifically occurs when trying to use Prisma's TypedSQL feature.
If you encounter this issue or have any insights, please open an issue in this repository or contribute to the discussion on the Prisma GitHub repository.