gregnavis / active_record_doctor

Identify database issues before they hit production.
MIT License
1.72k stars 56 forks source link

Fix `MissingNonNullConstraint` to work with `ActiveRecord.belongs_to_required_validates_foreign_key` #151

Closed fatkodima closed 2 weeks ago

fatkodima commented 11 months ago

Rails 7.1 introduced an optimization to required belongs_to association to not check for presence when foreign key's value hasn't changed (see https://github.com/rails/rails/pull/46522 for details).

In this PR I made active_record_doctor be aware of this configuration. We can assume, that if a validator has an :if condition and validation's message is :required and ActiveRecord.belongs_to_required_validates_foreign_key == false, then this condition was set with the rails itself (not a conditional set by the user).

fatkodima commented 2 weeks ago

@gregnavis Applied suggested changes.