P
Prisma3mo ago
aychar

Prisma keeps pasting incorrect schema

I'm admittedly not great with backend stuff, so I don't know if my Tournament table schema is bad, but after editing that table I keep having this weird issue where this clearly incorrect data keeps getting pasted into my User table on save. I've tested this on both VSCode and WebStorm, same thing happens. prisma and @prisma/client are on ^5.18.0 schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

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

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

model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

Tournament Tournament[]
Tournament Tournament? @relation(fields: [tournamentId], references: [id])
tournamentId String?
}

model Account {
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@id([provider, providerAccountId])
}

model Session {
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Tournament {
id String @id @default(uuid())
tournamentName String
tournamentAcronym String
hostId String
host User @relation(name: "TournamentHost", fields: [hostId], references: [id])
users User[] @relation(name: "TournamentUsers")
startDate DateTime
endDate DateTime
}
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

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

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

model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

Tournament Tournament[]
Tournament Tournament? @relation(fields: [tournamentId], references: [id])
tournamentId String?
}

model Account {
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@id([provider, providerAccountId])
}

model Session {
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Tournament {
id String @id @default(uuid())
tournamentName String
tournamentAcronym String
hostId String
host User @relation(name: "TournamentHost", fields: [hostId], references: [id])
users User[] @relation(name: "TournamentUsers")
startDate DateTime
endDate DateTime
}
Solution:
```tsx model User { id String @id @default(cuid()) tournamentsHosted Tournament[] @relation(name: "TournamentHost") tournamentsJoined Tournament[]...
Jump to solution
12 Replies
jonfanz
jonfanz3mo ago
So that’s expected (and correct!) behavior. https://www.prisma.io/docs/orm/prisma-schema/data-model/relations The thing to note there is this quote:
At a Prisma ORM level, a connection between two models is always represented by a relation field on each side of the relation.
Relations (Reference) | Prisma Documentation
A relation is a connection between two models in the Prisma schema. This page explains how you can define one-to-one, one-to-many and many-to-many relations in Prisma.
jonfanz
jonfanz3mo ago
The thing putting in those lines is our Prisma formatter, which is seeing the missing “back relation” fields and adding them
aychar
aycharOP3mo ago
ok i see, in that case should i ignore the linter complaining about the two Tournament fields? because every time i try to remove one of them it adds more fields automatically 😅
jonfanz
jonfanz3mo ago
You can change the names to something better. Maybe in your case it would be tournamentsHosted and tournamentsAttended?
aychar
aycharOP3mo ago
is this what you were talking about? whenever i try to change them, it adds the fields right back, and then if i delete the fields it added, it adds user fields to the tournament model
aychar
aycharOP3mo ago
im sorry i dont know if im doing something wrong im just so confused 😭
jonfanz
jonfanz3mo ago
Huh yeah that does look wrong. What version of the VSCode extension are you on?
aychar
aycharOP3mo ago
5.18.0
No description
jonfanz
jonfanz3mo ago
weird. Let me try and replicate this. Okay, figured it out 🙂 So you have two relations. One is "one-to-many" which is User<->Tournament (one user can host many tournaments) The other is "many-to-many" which is (also) User<->Tournament (one user can join many tournaments, each tournament has many users. Let's break down the two relations:
model User {
id String @id @default(cuid())
tournamentsHosted Tournament[]
}

model Tournament {
id String @id @default(uuid())
hostId String
host User @relation(fields: [hostId], references: [id])
}
model User {
id String @id @default(cuid())
tournamentsHosted Tournament[]
}

model Tournament {
id String @id @default(uuid())
hostId String
host User @relation(fields: [hostId], references: [id])
}
So that will be both directions of the first "one-to-many" relation. Now to add a many-to-many, we just have to add lists of models on both sides.
model User {
id String @id @default(cuid())
tournamentsHosted Tournament[]
tournamentsJoined Tournament[]
}

model Tournament {
id String @id @default(uuid())
hostId String
host User @relation(fields: [hostId], references: [id])
users User[]
}
model User {
id String @id @default(cuid())
tournamentsHosted Tournament[]
tournamentsJoined Tournament[]
}

model Tournament {
id String @id @default(uuid())
hostId String
host User @relation(fields: [hostId], references: [id])
users User[]
}
Now if you do that, you'll see an error under "tournamentsHosted" about an ambiguous relation. Then you just have to add a name to the host<->tournamentsHosted relation (like you had)
Solution
jonfanz
jonfanz3mo ago
model User {
id String @id @default(cuid())
tournamentsHosted Tournament[] @relation(name: "TournamentHost")
tournamentsJoined Tournament[]
}

model Tournament {
id String @id @default(uuid())
hostId String
host User @relation(name: "TournamentHost", fields: [hostId], references: [id])
users User[]
}
model User {
id String @id @default(cuid())
tournamentsHosted Tournament[] @relation(name: "TournamentHost")
tournamentsJoined Tournament[]
}

model Tournament {
id String @id @default(uuid())
hostId String
host User @relation(name: "TournamentHost", fields: [hostId], references: [id])
users User[]
}
There you go!
aychar
aycharOP3mo ago
it worked! thank you so much for your help, its greatly appreciated
jonfanz
jonfanz3mo ago
no prob
Want results from more Discord servers?
Add your server