-
-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullable belongsTo relationships #941
Comments
This will be a great improvement to the typings for Lucid relations. |
Agree, it will be nice to allow one to one relationships to be represented as null as well. Can you open a PR for the same? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Closing in favor of the PR. |
Relased as |
Why this feature is required (specific use-cases will be appreciated)?
In our table design, we sometimes have a nullable foreign key relationship with another table. In our models we then define this relationship with
belongsTo
. However, there does not seem to be any way to define the relationship as optional/nullable. While this isn't a breaking problem, it means downstream we can't rely on TypeScript to give us correct warnings where our relationship may be null.As an example, if you had a model
Issue
, you might have a column on that table forassigneeId
which is the user who is looking into the issue. While the issue is unassigned theassignee
relationship is null, so we should be writing code likeissue.assignee?.name
. As the relationship isn't nullable however, TypeScript does not know thatassignee
can be null, so it will not warn us if we do not do this safely.Similarly, it would also make sense to allow
HasOne
to be nullable.Have you tried any other work arounds?
I can't find anything in the docs suggesting this is possible. I've tried some variations on the relationship definition but they don't work:
Are you willing to work on it with little guidance?
Yes.
The text was updated successfully, but these errors were encountered: