prisma / studio

🎙️ The easiest way to explore and manipulate your data in all of your Prisma projects.
https://www.prisma.io/studio
1.81k stars 45 forks source link

"Message: Error in Prisma Client request" #1246

Closed bonsaideasea closed 1 month ago

bonsaideasea commented 1 month ago

Bug description

Screenshot 2024-05-23 at 2 30 41 PM Screenshot 2024-05-23 at 2 31 10 PM

I'm not even sure how to word the issue I'm running into because I can't decrypt where my errors are coming from in Prisma Studio. I am not receiving a specific error code but it seems like I haven't initialized my prisma/client properly?? Has anyone ever seen this before and how did you resolve it?

How to reproduce

Expected behavior

I should be able to open up my "Message" model in prisma studio to update my database.

Prisma information

<-- schema.prisma -->

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Message {
  id        String   @id @default(cuid()) 
  content   String 
  userId    String
  user      User     @relation(fields: [userId], references: [id])
  channelId String
  channel   Channel  @relation(fields: [channelId], references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

<-- client.ts -->

import { PrismaClient } from '@prisma/client'

const prismaClientSingleton = () => {
  return new PrismaClient()
}

type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClientSingleton | undefined
}

export const prisma = globalForPrisma.prisma ?? prismaClientSingleton()

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

<-- route.ts -->

import { NextResponse, NextRequest } from "next/server"; 
import schema from "./schema";
import prisma from "@prisma/client";

export async function GET(request: NextRequest) {
  const messages = await prisma.message.findMany({});
  return NextResponse.json(messages) 
}

Environment & setup

Screenshot of page where the problem happened (if applicable)

Screenshot 2024-05-23 at 2 39 49 PM

No response

Prisma logs

No response

janpio commented 1 month ago

userId and user are required, but the query that Studio runs returns data that is missing that and is null instead - and hence throws an error (top right image). You need to fix your data to match your Prisma schema to be able to use Prisma Studio.

bonsaideasea commented 1 month ago

hey janpio, i really appreciate you responding. i think my data does match my prisma schema. do you see any discrepancies that i can not spot? (what's not crossed out is fake data from my postgres GUI tool)

i saw that "field user is required to return data" which is why i filled all the columns with dummy data.

Screenshot 2024-05-23 at 7 02 02 PM Screenshot 2024-05-23 at 7 02 39 PM
janpio commented 1 month ago

user should not really be a field with data in your table, but only a relation defined via userId. You should have a User model with for example fields like name then.

You can run npx prisma db pull to update your Prisma schema to match your current database structure - that might help you understand what is going on.

bonsaideasea commented 1 month ago

we do have a User and Channel model that my groupmates are taking care of. i totally see what you are saying now and it makes a lot of sense that i'll be retrieving data from their models for those two columns. thank you so much janpio.

bonsaideasea commented 1 month ago

i deleted "user" and "channel" off my postgres & this solved my issue!! bless you janpio.