rubocop / rails-style-guide

A community-driven Ruby on Rails style guide
http://rails.rubystyle.guide
6.47k stars 1.06k forks source link

Use `add_reference`/`t.references` in migrations with `foreign_key: true` #287

Closed pirj closed 2 years ago

pirj commented 3 years ago

According to https://api.rubyonrails.org/v6.1/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_reference, foreign_key defaults to false.

When dependent: on belongs_to is not properly set, it may leave orphaned records behind on association deletion.

It also prevents from early detection of code responsible for referral integrity violations.

pirj commented 3 years ago

After looking more closely, this boils down to providing an example to https://github.com/rubocop/rails-style-guide#foreign-key-constraints section.