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.
What I tried to do
I was trying to make Token
model in Prisma for issuing token for registering new users.
What I actually tried
User
model
model User { id Int @id @default(autoincrement()) firstName String lastName String email String @unique phoneNumber String Token Token[]}
Token
model
model Token { id Int @id @default(autoincrement()) payload String @unique user User @relation(fields: [userId], references: [id]) createdAt DateTime @default(now()) updatedAt DateTime? @updatedAt userId Int}
Error that I got
It was not an error but there was warning sign on @relation
part.
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 (opens in a new tab)"
Analysis
According to the official documentation (opens in a new tab) of Prisma Client, as relational database uses foreign key constraint it implicitly creates foreign key column so as to quickly check data without table scan separately.
On the other hand, prisma
relation does not use foreign key constraint, and thus indexes are created after making migration to DB.
Therefore, we need to add foreign key field manually.
Solution
Before
I edited the previous Token
model
model Token { id Int @id @default(autoincrement()) payload String @unique user User @relation(fields: [userId], references: [id]) createdAt DateTime @default(now()) updatedAt DateTime? @updatedAt userId Int}
After
with adding @@index
(opens in a new tab) and included index
as documentation suggested.
model Token { id Int @id @default(autoincrement()) payload String @unique userId Int createdAt DateTime @default(now()) updatedAt DateTime? @updatedAt User User @relation(fields: [userId], references: [id]) @@index([userId])}
After
with adding @@index
(opens in a new tab) and included index
as documentation suggested.
model Token { id Int @id @default(autoincrement()) payload String @unique user User @relation(fields: [userId], references: [id]) createdAt DateTime @default(now()) updatedAt DateTime? @updatedAt userId Int}