-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Feature: Add Config Option to Enforce Nullable Relationships #1580
Feature: Add Config Option to Enforce Nullable Relationships #1580
Conversation
Introduce enforce_nullable_relationships configuration to control nullable Eloquent relationships.
Update isRelationNullable method to respect new config option.
- Verify behavior of enforce_nullable_relationships configuration option. - Create snapshot to reflect enforce_nullable_relationships set to false.
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.
LGTM!
Can you please add a changelog entry?
But isn't it only marked as nullable when the database column is nullable? So if it cannot be null, it will not add |
That is not true for the relationship object. This PR (#1231) introduced a situation where the column is marked required (not-nullable) and the package will identify it as null because it does not have a foreign key defined. This causes an inconvenience for any application that chooses not to have foreign keys and uses not-nullable columns. For clarification, it is the relationship object that is marked with |
We could use this, or something like this, for our use case. In our database, we can't comfortably use foreign key constraints. Unfortunately, they lead to significant complications with schema changes -- namely, we can no longer use gh-ost (GitHub does not use and therefore support foreign key constraints) and native MariaDB Additionally, we have a legacy codebase to support as we transition to Laravel, so we've got a whole host of things foreign key constraints would unfortunately complicate. I know less about the correctness/incorrectness of the mentioned PR, but at least the option to forego foreign key constraints and rely solely on the related column's nullability would be of significant value to us. Thank you!! |
@barryvdh I've noticed some releases coming through this month. Any chance this will make it in soon. I've resorted to a fork due to the bug existing. Also, I've answered your question above. Let me know if you have any other questions or if something doesn't make sense. Thanks. |
Summary
This PR introduces a new configuration option
enforce_nullable_relationships
to the Laravel IDE Helper package. The purpose of this option is to provide flexibility in how nullable Eloquent relationships are treated during static analysis, helping to avoid false positives when the application logic ensures the presence of related records.Rationale
The current behavior of the Laravel IDE Helper package can give unwanted null warnings in applications that ensure data integrity of relationships on non-nullable Eloquent relationship columns. Specifically, the static analysis tool may flag Eloquent relationship objects as nullable even when application logic guarantees that these relationships and their objects are never null. This behavior results in unnecessary and misleading warnings that disrupt development workflows.
This change addresses scenarios where the application logic guarantees the presence of related records, thus avoiding false positives in static analysis. By default, the option is set to
true
, preserving existing behavior, while allowing developers to disable it when needed.Issue at Hand
Changes Made
enforce_nullable_relationships
toconfig/ide-helper.php
isRelationNullable
method inModelsCommand.php
to consider the new configuration optionTests/Console/ModelsCommand/Relations/Test.php
to verify the behavior of theenforce_nullable_relationships
configuration optionenforce_nullable_relationships
is set tofalse
.Impact
Type of change
Checklist
composer fix-style