[Relational Queries] Nullable one-to-one relationship
Hey y'all.
I'm using the relational queries feature and have a one-to-one relationship between these two tables.
I have this query to fetch a user, their team (if they have one) and their team's project (if they have one).
This is the return type of that function.
The problem is, project could potentially be
null
if the team does not have a project.
I'm not really sure what to do here to tell TS that project can be null
. Am I creating the relations correctly or am I doing something wrong? How should I solve this?
Thank you in advance for the help and I apologize for the long question.6 Replies
cc: @Dan Kochetov
hi, I ran into the same thing, is there any way to make a nullable one relationship?
here's a smaller example schema:
I'm trying to be able to query like this:
and get typeof users:
instead of:
my actual use case is an
organization
table (the user
in the example) that can optionally have a config per country, so there are other tables like argentina_organization
and colombia_organization
with fks to organization
you should only provide
fields
and references
on the side that "knows" about both sides of the relation - i.e. in your case, on the users_settings
side. The one on the users
side should be just settings: one(user_settings)
. I think in that case it'll be recognized as nullable.
cc: @sexninewoah thanks it helped
I think you should explain this in the docs...
yep, we have it on the task list