Questions tagged [prisma]

Prisma is a next-generation Node.js and TypeScript ORM for PostgreSQL, MySQL, SQLite, CockroachDB, MongoDB and SQL Server.

Prisma is a next-generation ORM and can be used in any Node.js or TypeScript backend application (including serverless applications and microservices). This can be a REST API, a GraphQL API a gRPC API, or anything else that needs a database. Use the Prisma client to access your database with Node.js, TypeScript or Go.

Prisma features:

  • An auto-generated and type-safe client library
  • Declarative data modeling and migration system with Prisma Migrate
  • Beautiful data management workflows with Prisma Studio
3236 questions
12
votes
3 answers

Error when migrating models to database Prisma

I'm starting a project where I have to learn a new technology and I chose to build a full-stack app with Prisma and Next.js. I'm using both for the first time. I've built front-end apps w/ React.js and feel confident about using Next. However, I'm…
11
votes
3 answers

How do I use next auth getServerSession in next js 13 beta server component in app directory

I'm using next auth v4 with next js 13 beta with server component, and everything works fine. But I have a situation where I will need to know the logged user id, since I'm using next auth, I have access to the session, I can use useSession() but…
Shakib Hasan
  • 231
  • 1
  • 1
  • 8
11
votes
1 answer

How to find or create a record with Prisma?

Is this the best way to find or create a user in Prisma? prisma.user.upsert({ where: { id: user.id, }, update: { id: user.id, }, create: { id: user.id, }, })
grabury
  • 4,797
  • 14
  • 67
  • 125
11
votes
3 answers

How to set environment variables with Prisma, Nextjs, and Vercel

Nextjs wants you to use a .env.local file to store env vars. Prisma uses .env If I use a .env.local file then setting up the Prisma db datasource db { provider = "postgresql" url = env("DATABASE_URL") } I get a DATABASE_URL does not exist…
grabury
  • 4,797
  • 14
  • 67
  • 125
11
votes
3 answers

Testing a NestJS Service that uses Prisma without actually accessing the database

Most examples I've seen of how to test a Prisma-injected NestJS Service (e.g. prisma-sample in testing-nestjs) are for "end to end" testing. They actually access the database, performing actual queries and then rolling back the results if…
rinogo
  • 8,491
  • 12
  • 61
  • 102
11
votes
1 answer

Prisma how can I update only some of the models fields in update()

I have a Prisma model with lets say 10 fields. Think User model with firstname, lastname, address, e-mail , phone, mobile, age etc. I am trying to write a update method for this, where I most of the times only want to update some or only 1 of the…
skorpio
  • 179
  • 1
  • 6
11
votes
2 answers

LEFT JOINS and aggregation in a single Prisma query

I have a database with multiple tables that frequently need to be queried with LEFT JOIN so that results contain aggregated data from other tables. Snippet from my Prisma schema: model posts { id Int @id @unique…
James
  • 325
  • 1
  • 3
  • 15
11
votes
1 answer

Prisma - How to define compound unique constraint with fields in multiple models?

I have this not so straightforward model relationship in Prisma. User --< Enrollment >-- Course and I can't figure out how to ensure the Course title field is unique just among that user's created courses. In other words, I dont want one user to…
TemuujinNat
  • 135
  • 1
  • 1
  • 7
11
votes
5 answers

prisma findUnique where takes only one unique argument

I ran into an issue where I need to check if a user exists by his username and email since both are unique fields in the database, but I got an error. Argument where of type UserWhereUniqueInput needs exactly one argument, but you provided username…
ahmdtalat
  • 149
  • 1
  • 1
  • 7
11
votes
1 answer

One-to-many self-relation in prisma schema

I want to create a simple table: Users --- id name friends friends field should be an array of other user ids. I'm trying to define a schema for this in schema.prisma: model User { id String @id @default(uuid()) name String friends…
domnantas
  • 744
  • 1
  • 7
  • 11
11
votes
2 answers

Prisma client query for latest

Given the following schema. How would I query for the latest commit on each repository using the prisma2 client? model Commit { id String @default(uuid()) @id date DateTime @default(now()) } model Branch { id String …
TheAschr
  • 899
  • 2
  • 6
  • 19
10
votes
1 answer

How to make id autoincrement in schema.prisma?

I am writing a postgreSQL database. The ID must be auto-incrementing. model User { id String @id @default(cuid()) name String? email String? @unique emailVerified DateTime? @map("email_verified") image …
lian. lun
  • 115
  • 1
  • 1
  • 9
10
votes
2 answers

Which is better - Int autoincrement id or cuid for PostgreSQL prisma schema?

We use Postgres and prisma for our Next.js app. Previous developers have used cuid for every table on our schema. For some reasons we are restructuring the tables and I was wondering would it be better to use int ids? Would it result in any…
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
10
votes
1 answer

How to mutate user's session in nextauth when you change user data?

I want to update the user's data but after updating the user's data how to make also the change appear in session? [...nextauth].js callbacks: { jwt: ({ token, user }) => { if (user) { token.id = user.id; token.name =…
n9p4
  • 304
  • 8
  • 34
10
votes
2 answers

Update multiple rows using Prisma without manual loops

I have following prisma.schema: model Tag { id Int @id @default(autoincrement()) name String @unique files FileTag[] } model FileTag { tag Tag @relation(fields: [tagId], references: [id], onDelete: Cascade) tagId Int …
RajibTheKing
  • 1,234
  • 1
  • 15
  • 35