2

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.

Axel Padilla
  • 143
  • 7
Adrian Hossen
  • 21
  • 1
  • 3
  • [**Please Never** post images of or off site links to code, data or error messages](https://meta.stackoverflow.com/a/285557/2310830). Please edit your question and include copy/paste the text into the question, formatted. This is so that we can try to reproduce the problem without having to re-type everything, and your question can be properly indexed or read by screen readers. – RiggsFolly Dec 11 '22 at 19:53
  • At a guess, also dont know prisma, it need to know which table the `references:[id]` exists in! – RiggsFolly Dec 11 '22 at 19:55
  • I tried your schema file locally and didn't get any error. Can you try running `npx prisma format` and see if you still get the error? – Nurul Sundarani Dec 12 '22 at 08:44
  • Nothing :(. It gives me the following error: ```Error: foreign key constraints are not allowed, see https://vitess.io/blog/2021-06-15-online-ddl-why-no-fk/``` – Adrian Hossen Dec 12 '22 at 21:59
  • 1
    Seems like adding ```relationMode = "prisma"``` fixes my issue. Thank you both for your help :). – Adrian Hossen Dec 12 '22 at 22:07
  • I am glad to hear that the issue is resolved. – Nurul Sundarani Dec 13 '22 at 17:50

1 Answers1

11

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.

Axel Padilla
  • 143
  • 7