language-tools icon indicating copy to clipboard operation
language-tools copied to clipboard

Recommend adding indices to foreign keys when `referentialIntegrity = "prisma"`

Open 2color opened this issue 4 years ago • 1 comments

Problem

See the problem in more detail here https://github.com/prisma/prisma/issues/7292#issuecomment-963118192

Basically, when referentialIntegrity = "prisma" no foreign keys are created which means no index is created. When using with PlanetScale this leads to full table scans.

Suggested solution

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["referentialIntegrity"]
}

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

model Post {
  id       Int       @id @default(autoincrement())
  title    String
  excerpt  String
  content  String?
  views    Int       @default(0)
  likes    Int       @default(0)
  comments Comment[]
}

model Comment {
  id      Int    @id @default(autoincrement())
  comment String
  postId  Int
  post    Post   @relation(fields: [postId], references: [id], onDelete: Cascade)

  // ADD RECCOMENDATION TO ADD THIS INDEX
  @@index([postId])
}

Alternatives

Maybe there's a different way to suggest this? I'm not too familiar with all the capabilities of a VS Code plugin.

Additional context

https://briananglin.me/posts/spending-5k-to-learn-how-database-indexes-work/

2color avatar Dec 07 '21 11:12 2color

Related Issue https://github.com/prisma/prisma/issues/10611

Jolg42 avatar Dec 13 '21 09:12 Jolg42