Hey! Is it possible to limit choice of `User` `Pro...
# orm-help
a
Hey! Is it possible to limit choice of
User
Profile
based on
Profile[]
available in
School
?
✅ 1
m
Could you specify your use case a little more clearly?
a
I'd like to ensure that every
User
in
School
can only choose profile that
School
provides in
Profile[]
Or is it logical part that I cannot implement in the relation?
m
So - a School hasMany Users, a School hasMany Profiles, and a User hasOne Profile, and for every User, their Profile must be from their School - right?
a
Exacly!
m
that's a cool problem. I'm going to sit here and think on it a bit while I wait for an answer on some other thing. 🙂
👍 1
a
Oh thanks man, I appreciate that!
m
Consider two schools Westside Academy and Eastside Prep. Westside Academy has profiles "Tuition" and "Scholarship". Eastside Prep has profiles "Athlete" and "Nerd". So we have distinct profile names, which should make the problem easier to think about.
A User belongs to either Westside Academy or to Eastside Prep, but not both. If a User belongs to Westside Academy, then they definitely have the Profile Tuition or the Profile Scholarship.
👍 1
...
a
Of course it's easy to implement that on logical part of code or maybe in pure SQL, but I have no idea if I can make it out in prisma :DD
m
Yeah
I'm sitting here wondering if it's possible on the relations themselves.
a
Maybe that's more like case for noSQL db?
Hmm, theoretically, each school can have its own unique profiles
m
I'm not sure! I don't think I have any good advice, but if anyone else does, I hope they'll ping me in their response. Two things jump out at me about your current model, though. 1. If you want Users to have Profiles, you need to set a relation there. Unless you create some sort of three-way joining table. (schoolId, profileId, userId). 2. Just generally speaking, a phone_number could belong to a household, right? So what about the case of siblings - should the phone field really be unique? This is of course completely related to your business logic - just food for thought. 🙂
a
Aye thanks!
User
is actually a student so phone number in this case is pretty fine
Probably I should connect it with
Profile
simplest way, and then ensure that it's not possible to insert value with
Profile
that's outside school that
User
belongs to
m
Yeah. I have some similar relations, and if I were creating a User for a school, then I would get the school's profiles as a limiter and make sure they can't assign a profile from another school. I hope I haven't derailed your initial question too much, or that people pass it by because there's been such a long response. Good luck!