I am currently utilizing prisma in my project, which consists of three models:
model User {
id String @id @default(cuid())
name String @unique @db.VarChar(35)
email String @unique @db.VarChar(512)
password String @db.VarChar(1024)
details String @default("") @db.VarChar(512)
avatar String @default("/default-avatar.svg") @db.VarChar(150)
activity String @default("Online") @db.VarChar(25)
likes Int @default(0)
suggestions Boolean @default(false)
verified Boolean @default(false)
blockedUsers BlockedUser[]
comments Comment[]
communities Community[]
communityMembers CommunityMember[]
followers Follower[]
friends Friend[]
messages Message[]
posts Post[]
openDMs DM[]
interests UserInterest[]
@@map("user")
}
model Community {
id String @id @default(cuid())
title String @unique @db.VarChar(35)
details String @db.VarChar(512)
memberID String?
membersUser User[]
members CommunityMember[]
interests CommunityInterest[]
posts Post[]
@@map("community")
}
model CommunityMember {
id String @id @default(cuid())
nickname String?
userID String
communityID String
user User @relation(fields: [userID], references: [id])
community Community @relation(fields: [communityID], references: [id])
owner Boolean
@@map("community_member")
}
A specific route in my backend is causing an issue. It involves creating a new table for community members using the prisma client and connecting existing users and communities by their IDs. However, this process results in an error message:
Unique constraint failed on the constraint: community_member_communityID_key
Shown below is the code snippet responsible for adding a community member:
await prisma.communityMember.create({
data: {
nickname: response.account.name,
user: {
connect: { id: response.account.id }
},
community: {
connect: { id: communityID }
},
owner: false
}
});
I have attempted to drop the database and reset prisma migrations, but the problem persists. Upon inspecting the MySQL table, it appears that both the communityID
and userID
fields are set as unique, leading me to suspect an issue with prisma migrate.
Does anyone have insight into what might be causing this issue and how I can create these fields without encountering uniqueness constraints?