PRIMARY KEY in supabase DB

Hi everyone! I’m creating a full stack app with tRPC and I’ve connected Prisma to a supabase postgresql DB. Everything works fine but when I try to delete columns I have a error saying that I need a PRIMAY KEY. Can anyone please explain to me why I need that primary key in order to delete /edit my data in DB? 🙏 thanks
4 Replies
zenith
zenith3y ago
I'm confused, are you trying to delete the primary key or have you already deleted the primary key? You need a unique field for foreign key references in other tables Othwrwise your relational database becomes distinctly non relational which defeats the point
pogadev
pogadevOP3y ago
Thanks for the answer, @zenith I tired to delete a column from my table and I got a warning message that I need a primary key in order to be able to delete / edit columns. So I created a primary key and my question was why de we need a primary key, what’s the purpose 😆
zenith
zenith3y ago
Primary keys is the way that rows in other tables relate to that table Think of it just like an ID in fact, my PKs are usually just called id
model Study {
id String @id @default(cuid())
title String
description String?
authorId String
author User @relation(fields: [authorId], references: [id])
lessons Lesson[]
}

model Lesson {
id String @id @default(cuid())
title String
studyId String
study Study @relation(fields: [studyId], references: [id])
}
model Study {
id String @id @default(cuid())
title String
description String?
authorId String
author User @relation(fields: [authorId], references: [id])
lessons Lesson[]
}

model Lesson {
id String @id @default(cuid())
title String
studyId String
study Study @relation(fields: [studyId], references: [id])
}
Here's an example
pogadev
pogadevOP3y ago
Thanks a lot for this answer
Want results from more Discord servers?
Add your server