开发者

explicit many to many self-relations produces weird results in the generated client, pairing up wrong values

开发者 https://www.devze.com 2022-12-07 22:00 出处:网络
I\'ve got users and contact requests with a table in between to explicitly define a self relation for cascade purposes.

I've got users and contact requests with a table in between to explicitly define a self relation for cascade purposes.

user schema:

  contactRequestsRecei开发者_如何学JAVAvedFrom ContactRequestRelations[] @relation("contactRequestReceivedFrom")
  contactRequestsMadeTo       ContactRequestRelations[] @relation("contactRequestMadeTo")

relations schema:


model ContactRequestRelations {
  contactRequestReceivedFrom User   @relation("contactRequestReceivedFrom", fields: [requestReceivedFromId], references: [id], onDelete: Cascade)
  contactRequestMadeTo       User   @relation("contactRequestMadeTo", fields: [requestMadeToId], references: [id], onDelete: Cascade)
  requestReceivedFromId      String
  requestMadeToId            String

  @@id([requestReceivedFromId, requestMadeToId])
}

• I have to give the relations a name, prisma will complain it's too ambiguous otherwise • if I give the same relation reference on all 4 of them, prisma will complain of "wrongly named relation detected"

I wanted to make a 'send contact request' operation.

Where I find either the user that the request is made for and then add the relation to the 'contactRequestsReceivedFromarray ---- or ---- find the user that is sending the request and create a relation on thecontactRequestsMadeTo`.

The issue is, even though the relation contactRequestsReceivedFrom ContactRequestRelations[] @relation("contactRequestReceivedFrom") on the user is clearly paired with the contactRequestReceivedFrom User @relation("contactRequestReceivedFrom", fields: [requestReceivedFromId], references: [id], onDelete: Cascade) relation on the relation table, when I attempt to create this with the client, prisma only lets me define the opposite of that relation which is rather redundant and confusing:

explicit many to many self-relations produces weird results in the generated client, pairing up wrong values

as it would serve no purpose to define in that context, as I would be defining who made the request.

Is this the correct pattern/syntax, or should I be looking at a more complex model with a couple more tables in between? Not a big fan of that option.

0

精彩评论

暂无评论...
验证码 换一张
取 消