I have a few tables and I’m getting an error that ...
# orm-help
i
I have a few tables and I’m getting an error that I don’t quite follow
Copy code
model Item {
  id       String @id @default(cuid()) 
  age    String

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  publishers  Publisher[]

}

Model Publisher {
  id            String @id @default(cuid()) 
  name          String @unique

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  media Item[]
}
When I try to create/upsert an Item, I get the error that publisher
name
is not unique (
Unique constraint failed on the fields: ("name")
. However, I’m using
connectOrCreate
within the upsert which means (in my understanding) if the current transaction has found that
name
already exists it should
connect
it (and not try to create it, which might be causing the error). Regardless, I’m trying to figure out where I’m wrong or what I can do to investigate more but so far I don’t understand how to solve this. I’d appreciate any help/thoughts from anyone 👍 (I have tried regenerating and that has not made a difference)
1
I’m not sure if it matters but I am doing multiple
connectOrCreate
within the upsert for the
Item
. I would assume that shouldn’t make a difference, but in case it does, I want to make it clear Update: That _*could*_ be the issue 🤔
Yea, the issue is that race condition happening. In testing with multiple upserts, it’s clear this only happens on random ones. Others are created just fine. For handling errors, this was helpful
💯 1
v
👋 Hello @Isaac Curtiss thank you for sharing the solution and I'm very glad to see you resolved this issue! I'm very curious about your use case, is this for an app in production? Can you tell us a little more about your tech stack?
i
Hey! Yea, sure I was working on a quick proof of concept/quick MVP with the RemixJS framework. This was not for a production application 👍
v
Ah I see! Did you end up having a successful MVP with Prisma and Remix?
i
Yep! I was able to get it working
💯 1