Im confused with many-to-many relation. Why no “cr...
# prisma-whats-new
n
Im confused with many-to-many relation. Why no “cross_table” is created?
y
@nosykretts There’s no need for a cross or lookup table because GraphQL is able to keep track of the relations directly. So if you queried a node that contained a many-to-many relationship the result in that relationship would come back as an array of objects.
Does that help?
n
I need to add field beetwen the m:n relation.
y
Could you post an example of the schema you’re wanting?
n
I have Patient, Exam, and Branch Type. Branch m:n Exam My business logic is. Every Branch may have different Exam pricing.
y
Got it! If I understand correctly, then you would want to set up your Nodes like this:
Copy code
type Patient implements Node {
  exams: [Exam!]! @relation(name: "ExamOnPatient")
  name: String!
  etc.
}

type Exam implements Node {
  price: Int!
  branchTypes: [BranchType!]! @relation(name: "BranchTypeOnExam")
  patient: Patient! @relation(name: "ExamOnPatient")
  etc.
}

type BranchType implements Node {
  name: String!
  exams: [Exam!]! @relation(name: "BranchTypeOnExam")
  etc.
}
n
wait a minute. I need time for processing that info 😁
y
Sorry, hit Enter instead of Shift-Enter at first. It’s all there now.
You would relate Patient to Exams, and then Exams to Branch Types.
Your
price
field would live in Exam, not BranchType, so that you could have the different prices.
n
I experimenting with your suggestion. need little time #youAreFast #ImSlowLearner 😃 👍
y
No problem!
n
@yergi If I understand correctly. suggestion you give me Branch and Exam still m:n relation?
y
Yes, it’s still a many-to-many relationship. Is that not what you need?
n
yes. but I still cannot set price on exam based on different branches
y
Since
price
is a field of
Exam
, whatever your system is where you’re creating the mutation calls to create new data should be able to set the
price
field based on the branches you’re relating the new data to.
n
so your suggestion I can create this? Branch A - Exam1 : 1200 - Exam2 : 1300 Branch B - Exam1 : 1900 - Exam2 : 1400
y
That’s right.
n
so my exam type is not unique?
y
What I gave you is the schema; you’ll still need to work out the logic in your solution on how to create the mutation calls to create new exams based on branch.
Hold on, sorry.
You’re wanting one single exam to have multiple prices, and each price be attached to a branch, is that accurate?
n
yes. every item on exam name should unique.
y
🤔
Sorry, I misunderstood that part.
In that case, you’d probably want an
ExamPrice
node, that has the
price
field, and that joins on a one-to-many relationship to
BranchType
.
And you would create a one-to-many relationship from
Exam
to
ExamPrice
.
So each
Exam
would have multiple prices, but each
ExamPrice
would belong to a single exam.
n
Yes. thats is my original question. I should make my own cross_table.
y
Yes, sorry. I misunderstood what part needed to be unique.
n
But. Im very thankful you help me out 👍
n
@nosykretts @yergi did you resolve this? Happy to help you out here 🙂