-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(repository): belongsTo accessor should return undefined if foreign key is not included #4325
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
Conversation
jannyHou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 with a suggestion.
| const sourceModel = await sourceRepository.findById(sourceId); | ||
| const foreignKeyValue = sourceModel[foreignKey as keyof Source]; | ||
| // different dbs use different empty values | ||
| if (foreignKeyValue === undefined || foreignKeyValue === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe use a falsy check like if(!foreignKeyValue) {}?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@jannyHou Another thing is, we didn't notice that we already have #2333 opened. #4147 is duplicate. I don't know if I should continue on this PR. Because this 'fix' will change some behavior of some connectors ( please the proposal above).
So I am thinking to close this PR without merging it. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@agnes512 I think #2333 and #4147 are talking about different issues:
2333: means when CREATE related item, if you provide a non-existing fk(like you have users with id 1 through 10, but you create an order for user with id=11), the record will still be created, which is wrong. The solution would be checking a fk's corresponding source model instance exists in db first, then perform the creation. The story is more like an improvement of our design.
4147: looks like a bug, when GET related item, the included items are wrong. like a todo item with id=1 doesn't belong to any todoList, which means it has an empty fk, but GET todos/1/todoList returns a todo list...and please note the WRONGLY returned todo list does exist in the db, the problem is it doesn't own the todo specified in the request path...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Back to this PR, since the belongsTo relation only has a get method(because child only has the get access to its parent, but cannot modify it), I am good with your approach here :) just return an empty result when fk is missing.
Something you may want to double check: should it return an empty ARRAY or just null/undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. I think the belongsTo accessor only checks the existence. And hasMany and hasOne do creation and traverse.
As for the return, I think undefined is better than [].
Once we done the memory connector referential integrity, this workaround should be removed, and let each connector to handle it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for the return, I think
undefinedis better than[].
In the PR description you said the connectors return [], so should we try to be consistent with that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nabdelgadir that was my proposal before. But I think Janny's suggestion makes more sense. Will update the description and the title later
| const sourceModel = await sourceRepository.findById(sourceId); | ||
| const foreignKeyValue = sourceModel[foreignKey as keyof Source]; | ||
| // workaround to check referential integrity. | ||
| // should be removed once the memory connector ref integrity is done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a link to the issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion
…n key is not included
fixes #4147
Relates to #2333
Current behavior
For instances:
The result of await
orderRepo.customer(order.id = 3);returns the first customer in the database even it doesn't have the foreign keyProposal
After reading through #2333, this is only a workaround.
Should check the foreign key value of the source instance.Since different databases use different empty value, we check both
undefinedandnull.I think this issue can be closed as duplicate..
[]in this case.with id "constraint {\"id\":null}". With the change, they return[].Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated👉 Check out how to submit a PR 👈