How do you create a database using prisma in nextjs
Unanswered
Pomeranian posted this in #help-forum
PomeranianOP
I wanted to create an api through prisma in nextjs but struggling to create a database. ive followed through the documentation but got stuck around step 2.1. i want to create a database locally so that i can simulate sending data via postman.
10 Replies
Sun bear
which step is 2.1 ? can you paste that here? creating a database in prisma just requires you to create the schema file, connect to your database, run migrate, and then create the prisma client to use it in next
import { PrismaClient } from "@prisma/client";
import "server-only";
declare global {
var prisma: PrismaClient | undefined;
}
export const prisma =
global.prisma ||
new PrismaClient({
log: ["query"],
});
if (process.env.NODE_ENV !== "production") global.prisma = prisma;@Pomeranian I wanted to create an api through prisma in nextjs but struggling to create a database. ive followed through the documentation but got stuck around step 2.1. i want to create a database locally so that i can simulate sending data via postman.
Azawakh
I can help to create a database in local
@Azawakh I can help to create a database in local
Munchkin
can you help me bro? i tried to follow the docs, but then at step 8 which when im trying to run
npx tsx script.ts i always failedwhats the error message
@Sun bear where does it fail
Munchkin
Invalid `prisma.user.create()` invocation in
/Users/bryanheinz/Documents/KULIAH/RPL/tubes/tubes-rpl/script.ts:5:34
2
3 async function main() {
4 // Create a new user with a post
→ 5 const user = await prisma.user.create(
at $r.handleRequestError (/Users/bryanheinz/Documents/KULIAH/RPL/tubes/tubes-rpl/node_modules/.pnpm/@prisma+client@7.0.1_prisma@7.0.1_@types+react@19.2.6_react-dom@19.2.0_react@19.2.0__re_10277e9ef291c66d7e9b5164d1cb4394/node_modules/@prisma/client/src/runtime/RequestHandler.ts:228:13)
at $r.handleAndLogRequestError (/Users/bryanheinz/Documents/KULIAH/RPL/tubes/tubes-rpl/node_modules/.pnpm/@prisma+client@7.0.1_prisma@7.0.1_@types+react@19.2.6_react-dom@19.2.0_react@19.2.0__re_10277e9ef291c66d7e9b5164d1cb4394/node_modules/@prisma/client/src/runtime/RequestHandler.ts:174:12)
at $r.request (/Users/bryanheinz/Documents/KULIAH/RPL/tubes/tubes-rpl/node_modules/.pnpm/@prisma+client@7.0.1_prisma@7.0.1_@types+react@19.2.6_react-dom@19.2.0_react@19.2.0__re_10277e9ef291c66d7e9b5164d1cb4394/node_modules/@prisma/client/src/runtime/RequestHandler.ts:143:12)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async a (/Users/bryanheinz/Documents/KULIAH/RPL/tubes/tubes-rpl/node_modules/.pnpm/@prisma+client@7.0.1_prisma@7.0.1_@types+react@19.2.6_react-dom@19.2.0_react@19.2.0__re_10277e9ef291c66d7e9b5164d1cb4394/node_modules/@prisma/client/src/runtime/getPrismaClient.ts:766:24)
at async main (/Users/bryanheinz/Documents/KULIAH/RPL/tubes/tubes-rpl/script.ts:5:16) {
code: 'ECONNREFUSED',
meta: { modelName: 'User' },
clientVersion: '7.0.1'
}Scaly-naped Pigeon
try
npx prisma studio, can you see you table User there? also check access permission to the database file if you use sqliteAmerican black bear
use docker