[10.x] Add HasOneOrMany::shouldAddForeignKeyCheck()
#46916
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#46909
HasOneOrMany adds a
where foreign_key is not null
to all queries, though in most cases it's not needed.This issue comes up a lot: it makes it hard to debug queries, it's unnecessary in most (all?) cases, and it leads to confusion from developers trying to figure out WHY it was added (if they did something wrong, where it's being added within the framework, etc).
Here is a solution that allows developers to turn it off entirely, or per relationship
Voila. Now instead of:
'select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null'
you get
'select * from "posts" where "posts"."user_id" = ?
Don't want it turned off globally? Cool, nothing is different.
Want it turned on globally but want it turned off for some relationship? Have at it.
In a future release, this static property could be set to false by default, and then maybe in a subsequent release, removed entirely.
final thought
I like how the DB migration traits within test uses a state class with static properties. Would be nice to see this bound to the container so it could be easily swapped within tests. But I don't see this pattern used elsewhere within the framework, so I assumed something so far out of the usual stood less of a chance of being accepted (especially for an issue that has been a long-standing).