Skip to content
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

Closed
waffleau opened this issue Jun 18, 2023 · 6 comments
Closed

Nullable belongsTo relationships #941

waffleau opened this issue Jun 18, 2023 · 6 comments
Assignees
Labels
Semver: Minor Mainly new features and improvements Status: Accepted The proposal or the feature has been accepted for the future versions Type: Enhancement Improving an existing feature

Comments

@waffleau
Copy link

waffleau commented Jun 18, 2023

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 for assigneeId which is the user who is looking into the issue. While the issue is unassigned the assignee relationship is null, so we should be writing code like issue.assignee?.name. As the relationship isn't nullable however, TypeScript does not know that assignee 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:

@belongsTo(() => User | null)
public user: BelongsTo<typeof User | null> = null

Are you willing to work on it with little guidance?

Yes.

@ndianabasi
Copy link

This will be a great improvement to the typings for Lucid relations.

@thetutlage
Copy link
Member

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?

@stale
Copy link

stale bot commented Sep 17, 2023

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.

@stale stale bot added the Status: Abandoned Dropped and not into consideration label Sep 17, 2023
@RomainLanz RomainLanz added Semver: Minor Mainly new features and improvements Status: Accepted The proposal or the feature has been accepted for the future versions and removed Status: Abandoned Dropped and not into consideration labels Sep 18, 2023
@RomainLanz
Copy link
Member

Closing in favor of the PR.

@RomainLanz
Copy link
Member

Relased as 18.4.1

@Marian0
Copy link

Marian0 commented Nov 10, 2023

hey @waffleau

Was this resolved in the 18.4.1 ?

I'm also wondering if you experience this behavior with preloads described here: #973

It may be related.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Semver: Minor Mainly new features and improvements Status: Accepted The proposal or the feature has been accepted for the future versions Type: Enhancement Improving an existing feature
Projects
None yet
Development

No branches or pull requests

5 participants