Foreign key constraints are not allowed (Prisma)

5.6k Views Asked by At

Currently facing an issue whilst attempting to push schema changes to planetscale db. Not sure what I may be doing wrong. I am fairly new to prisma, so I would appreciate some help :).

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native"]
}

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

model User {
  id    String @id @default(uuid())
  notes Note[]
}

model Note {
  id     String @id @default(uuid())
  userId String @unique
  user   User   @relation(fields: [userId], references: [id])
}

Tried migrating. If the relation of one to many is removed, the error goes away.

1

There are 1 best solutions below

0
On

In your schema.prisma file

datasource db {
  provider     = "mysql"
  url          = env("DATABASE_URL")
  
  // Add this
  relationMode = "prisma"
}

The reason can be found in the prisma documentation for relationMode. It states the following:

For relational databases, the available options are:

  • foreignKeys: this handles relations in the database with foreign keys. This is the default option for all relational database connectors and is active if no relationMode is explicitly set in the datasource block.

  • prisma: this emulates relations in the Prisma client. You should also enable this option when you use the MySQL connector with a PlanetScale database.