Меню ▼

Design ⚡️ Life

Alex Buznik Logbook

Strange case with prisma foreign key error constraint failed on applicationId P2003

Hi.

Thought I should share this.
I’ve done some development on a pet project using prisma and MySQL.

I added a new model with a relation and was getting an error prisma foreign key error constraint failed on applicationId P2003 when trying to create this new model even though the code and schema looked ok.

The fix was to rename the reference field to a lower case, so it’s different from the actual model name. Probably trivial for someone, but I haven’t any explicit notes against it in the docs.

model Invoices {
  id            Int             @id @default(autoincrement())
  ...
  // works:
  application   Application     @relation(fields: [applicationId], references: [id], onDelete: NoAction, onUpdate: Cascade) 

  // does not wrk:
  Application   Application     @relation(fields: [applicationId], references: [id], onDelete: NoAction, onUpdate: Cascade) 

  applicationId Int
}

Spelling error report

The following text will be sent to our editors: