P
Prisma•5d ago
harrishawker

The relation field on model is missing an opposite relation field on the model

Hey ya'll. Not sure how to describe this, so I'll just post my code and issue.
generator client {
provider = "prisma-client-js"
}

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

enum Sex {
suspected_male
suspected_female
male
female
intersex
unknown
}

model Animal {
id Int @id @default(autoincrement())
species String @db.VarChar(255)
spcies_scientific_name String @db.VarChar(255)
age String @db.VarChar(255)
dob DateTime?
sex Sex

mother Animal? @relation(name: "Mother", fields: [motherId], references: [id])
motherId Int? // relation scalar field (used in the `@relation` attribute above)

father Animal? @relation(name: "Father", fields: [fatherId], references: [id])
fatherId Int? // relation scalar field (used in the `@relation` attribute above)
}
generator client {
provider = "prisma-client-js"
}

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

enum Sex {
suspected_male
suspected_female
male
female
intersex
unknown
}

model Animal {
id Int @id @default(autoincrement())
species String @db.VarChar(255)
spcies_scientific_name String @db.VarChar(255)
age String @db.VarChar(255)
dob DateTime?
sex Sex

mother Animal? @relation(name: "Mother", fields: [motherId], references: [id])
motherId Int? // relation scalar field (used in the `@relation` attribute above)

father Animal? @relation(name: "Father", fields: [fatherId], references: [id])
fatherId Int? // relation scalar field (used in the `@relation` attribute above)
}
The relation field `mother` on model `Animal` is missing an opposite relation field on the model `Animal`.
The relation field `father` on model `Animal` is missing an opposite relation field on the model `Animal`.
The relation field `mother` on model `Animal` is missing an opposite relation field on the model `Animal`.
The relation field `father` on model `Animal` is missing an opposite relation field on the model `Animal`.
How do I solve this? I'm aware it needs to opposite relation field, but that doesn't really work here...
2 Replies
RaphaelEtim
RaphaelEtim•4d ago
Hi @harrishawker 👋 Are you trying to create a one to one self relation or a one to many self relation? For one to one self relation, this could work
enum Sex {
suspected_male
suspected_female
male
female
intersex
unknown
}

model Animal {
id Int @id @default(autoincrement())
species String @db.VarChar(255)
spcies_scientific_name String @db.VarChar(255)
age String @db.VarChar(255)
dob DateTime?
sex Sex

mother Animal? @relation(name: "Mother", fields: [motherId], references: [id])
motherId Int? @unique // relation scalar field (used in the `@relation` attribute above)
motheredAnimal Animal? @relation(name: "Mother") // opposite relation field

father Animal? @relation(name: "Father", fields: [fatherId], references: [id])
fatherId Int? @unique // relation scalar field (used in the `@relation` attribute above)
fatheredAnimal Animal? @relation(name: "Father") // opposite relation field
}
enum Sex {
suspected_male
suspected_female
male
female
intersex
unknown
}

model Animal {
id Int @id @default(autoincrement())
species String @db.VarChar(255)
spcies_scientific_name String @db.VarChar(255)
age String @db.VarChar(255)
dob DateTime?
sex Sex

mother Animal? @relation(name: "Mother", fields: [motherId], references: [id])
motherId Int? @unique // relation scalar field (used in the `@relation` attribute above)
motheredAnimal Animal? @relation(name: "Mother") // opposite relation field

father Animal? @relation(name: "Father", fields: [fatherId], references: [id])
fatherId Int? @unique // relation scalar field (used in the `@relation` attribute above)
fatheredAnimal Animal? @relation(name: "Father") // opposite relation field
}
While for one to many self relation, this can also work
enum Sex {
suspected_male
suspected_female
male
female
intersex
unknown
}

model Animal {
id Int @id @default(autoincrement())
species String @db.VarChar(255)
spcies_scientific_name String @db.VarChar(255)
age String @db.VarChar(255)
dob DateTime?
sex Sex

mother Animal? @relation(name: "Mother", fields: [motherId], references: [id])
motherId Int? // relation scalar field (used in the `@relation` attribute above)
motheredAnimal Animal[] @relation(name: "Mother") // opposite relation field

father Animal? @relation(name: "Father", fields: [fatherId], references: [id])
fatherId Int? // relation scalar field (used in the `@relation` attribute above)
fatheredAnimal Animal[] @relation(name: "Father") // opposite relation field
}
enum Sex {
suspected_male
suspected_female
male
female
intersex
unknown
}

model Animal {
id Int @id @default(autoincrement())
species String @db.VarChar(255)
spcies_scientific_name String @db.VarChar(255)
age String @db.VarChar(255)
dob DateTime?
sex Sex

mother Animal? @relation(name: "Mother", fields: [motherId], references: [id])
motherId Int? // relation scalar field (used in the `@relation` attribute above)
motheredAnimal Animal[] @relation(name: "Mother") // opposite relation field

father Animal? @relation(name: "Father", fields: [fatherId], references: [id])
fatherId Int? // relation scalar field (used in the `@relation` attribute above)
fatheredAnimal Animal[] @relation(name: "Father") // opposite relation field
}
motheredAnimal and fatheredAnimal are the opposite relation fields for mother and father, respectively. This ensures that the relations are properly defined and bidirectional. You can choose any name that best fit your application's domain.
harrishawker
harrishawker•4d ago
cool yeah that works ty!
Want results from more Discord servers?
Add your server