missing an opposite relation field

In a marketplace app, I have a JobAd entity that is created by an owner of type User. At some point in time after posting the JobAd, another User locks herself as the provider for this JobAd. I believe the entity definition should be entity JobAd {=psl id Int @id @default(autoincrement()) description String price Float isDone Boolean @default(false) createdAt DateTime @default(now()) owner User @relation(name: "createdBy", fields: [ownerId], references: [id]) ownerId Int provider User? @relation(name: "servedBy", fields: [providerId], references: [id]) providerId Int?
psl=} entity User {=psl id Int @id @default(autoincrement()) isProvider Boolean @default(false) jobAds JobAd[] psl=} However, when I run wasp db migrate-dev, I get the following error P1012 Error validating field jobAds in model User: The relation field jobAds on model User is missing an opposite relation field on the model JobAd. Either run prisma format or add it manually. Error validating field owner in model JobAd: The relation field owner on model JobAd is missing an opposite relation field on the model User. Either run prisma format or add it manually. Error validating field provider in model JobAd: The relation field provider on model JobAd is missing an opposite relation field on the model User. Either run prisma format or add it manually. How can I fix it?
3 Replies
kapa.ai
kapa.ai6mo ago
Hi @OrBlatt. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The error you're encountering is due to missing opposite relation fields in your User and JobAd entities. In Prisma, when you define a relation between two models, you need to specify the relation in both models. In your case, you have a User model that can be an owner of multiple JobAds and also a provider for multiple JobAds. You need to define these relations in your User model. Here is how you can modify your User entity:
entity User {=psl
id Int @id @default(autoincrement())
isProvider Boolean @default(false)
jobAdsOwned JobAd[] @relation(name: "createdBy")
jobAdsProvided JobAd[] @relation(name: "servedBy")
psl=}
entity User {=psl
id Int @id @default(autoincrement())
isProvider Boolean @default(false)
jobAdsOwned JobAd[] @relation(name: "createdBy")
jobAdsProvided JobAd[] @relation(name: "servedBy")
psl=}
And your JobAd entity should look like this:
entity JobAd {=psl
id Int @id @default(autoincrement())
description String
price Float
isDone Boolean @default(false)
createdAt DateTime @default(now())
owner User @relation(name: "createdBy", fields: [ownerId], references: [id])
ownerId Int
provider User? @relation(name: "servedBy", fields: [providerId], references: [id])
providerId Int?
psl=}
entity JobAd {=psl
id Int @id @default(autoincrement())
description String
price Float
isDone Boolean @default(false)
createdAt DateTime @default(now())
owner User @relation(name: "createdBy", fields: [ownerId], references: [id])
ownerId Int
provider User? @relation(name: "servedBy", fields: [providerId], references: [id])
providerId Int?
psl=}
This way, you're defining two separate relations in the User model: jobAdsOwned and jobAdsProvided, which correspond to the owner and provider relations in the JobAd model. This solution is based on the information provided in the Discord support thread: .wasp not recognising disambiguating relations (https://discord.com/channels/686873244791210014/1217557267663159346). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @OrBlatt, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
OrBlatt
OrBlattOP6mo ago
This is perfect, thank you
martinsos
martinsos6mo ago
Nice job Kapa!
Want results from more Discord servers?
Add your server