Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite Primary Key with relationMode Prisma and Foreign Key #1387

Open
varugasu opened this issue Mar 7, 2023 · 4 comments · Fixed by prisma/prisma-engines#4376
Open
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: referentialIntegrity/relationMode

Comments

@varugasu
Copy link

varugasu commented Mar 7, 2023

Bug description

I have a table with a composite primary key, and one of these keys is also a foreign key:

generator client {
    provider = "prisma-client-js"
}

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

model Restaurant {
    id                   BigInt                 @id @default(autoincrement())
    RestaurantPageImages RestaurantPageImages[]

    @@index([externalId])
}

model RestaurantPageImages {
    restaurantId BigInt
    imageId      Int
    url          String
    Restaurant   Restaurant @relation(fields: [restaurantId], references: [id])

    @@id([restaurantId, imageId])
}

Yet, I still got the warning on the line Restaurant Restaurant @relation(fields: [restaurantId], references: [id]):

With `relationMode = "prisma"`, no foreign keys are used, so relation fields will not benefit from the index usually created by the relational database under the hood. This can lead to poor performance when querying these fields. We recommend adding an index manually. Learn more at https://pris.ly/d/relation-mode-prisma-indexes" 

However, this is misleading because we would not have performance issues since the restaurantId is used as a hash key on the composite key

How to reproduce

  1. Have the Prisma VSCode plugin installed
  2. Use the Prisma file above

Expected behavior

No warning is shown

Prisma information

Prisma 4.9.0

Environment & setup

  • OS: Mac OS
  • Editor: VSCode
  • Editor version: 1.76.0
  • Extension version:v4.11.0

@Druue Druue added kind/bug A reported bug. team/schema Issue for team Schema. bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Mar 15, 2023
@Druue
Copy link
Contributor

Druue commented Mar 15, 2023

Hey, I get the following error with the schema posted above, am I missing something?
Error validating model Restaurant: The index definition refers to the unknown fields: externalId.

Druue added a commit to Druue/prisma-debug that referenced this issue Mar 15, 2023
@varugasu
Copy link
Author

My bad, @Druue . I deleted some data to make it readable but left some garbage. You can remove the @@index([externalId]) to reproduce the warning:

generator client {
    provider = "prisma-client-js"
}

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

model Restaurant {
    id                   BigInt                 @id @default(autoincrement())
    RestaurantPageImages RestaurantPageImages[]
}

model RestaurantPageImages {
    restaurantId BigInt
    imageId      Int
    url          String
    Restaurant   Restaurant @relation(fields: [restaurantId], references: [id])

    @@id([restaurantId, imageId])
}

@Druue
Copy link
Contributor

Druue commented Mar 16, 2023

Ah, yep!
image

@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Mar 16, 2023
@Vap0r1ze
Copy link

I also have this issue with this schema

generator client {
    provider = "prisma-client-js"
}

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

model User {
    id        String   @id @default(uuid())
    name      String
    image     String
    createdAt DateTime @default(now())

    ownedLobbies Lobby[]
    playerships  Playership[]
    decks        Deck[]
}

model Playership {
    lobbyId String
    userId  String
    lobby   Lobby  @relation(fields: [lobbyId], references: [id], onDelete: Cascade)
    user    User   @relation(fields: [userId], references: [id], onDelete: Cascade)

    joinedAt    DateTime @default(now())
    permissions Int

    @@id([lobbyId, userId])
}

model Deck {
    name      String
    createdAt DateTime @default(now())

    owner   User   @relation(fields: [ownerId], references: [id], onDelete: Cascade)
    ownerId String

    @@id([ownerId, name])
}

model Lobby {
    id        String   @id
    createdAt DateTime @default(now())

    ownerId     String
    owner       User         @relation(fields: [ownerId], references: [id], onDelete: Cascade)
    playerships Playership[]

    @@index([ownerId])
}

yubrot added a commit to yubrot/prisma-engines that referenced this issue Oct 23, 2023
Druue pushed a commit to prisma/prisma-engines that referenced this issue Mar 19, 2024
* Fix `relation_fields` validation to accept primary key indexes

This resolves prisma/language-tools#1387

---------

Co-authored-by: Jan Piotrowski <piotrowski+github@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: referentialIntegrity/relationMode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants