1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/guides
Alex Ghiculescu 47467fe33d Verify foreign keys after loading fixtures
When writing fixtures, it's currently possible to define associations that don't exist, even if a foreign key exists. For example:

```yml
george:
  name: "Curious George"
  pirate: redbeard

blackbeard:
  name: "Blackbeard"
 ```

When the fixtures are created, `parrots(:george).pirate` will be nil, but it's not immediately clear why. This can make it hard to debug tests and can give false confidence in passing ones.

This can happen because Rails [disables referential integrity](f263530bf7/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb (L407)) when inserting fixtures. This makes the fixtures algorithm much simpler - it can just create the fixtures in alphabetical order and assume that the other side of a foreign key constraint will *eventually* be added.

Ideally we would check foreign keys once all fixtures have been loaded, so that we can be sure that the foreign key constraints were met. This PR introduces that. To enable it:

```ruby
config.active_record.verify_foreign_keys_for_fixtures = true
```

I'm proposing we enable this in 7.0 for new apps and have added it to new framework defaults. When run against our app, it found 3 fixture files with unmet FK constraints - turns out all those fixtures weren't being used and were safe to delete.
2021-07-07 15:41:05 -05:00
..
assets [ci skip] Kindle info icon fix 2021-06-07 16:10:24 -03:00
bug_report_templates Fix bug report templates that are failing to run because rails 6.1.0 is dependant on mimemagic 2021-06-30 21:22:33 +01:00
rails_guides use ruby 2.7's filter_map instead of select + map 2021-04-26 17:55:33 -04:00
source Verify foreign keys after loading fixtures 2021-07-07 15:41:05 -05:00
.document
CHANGELOG.md ActiveStorage: The parameters sent to ffmpeg for are now configurable 2021-06-16 14:15:34 +12:00
rails_guides.rb Use safe nav operator when calling RailsGuides::Generator.new 2018-12-05 12:41:07 +09:00
Rakefile Use Bundler.unbundled_system for bug_report_templates tests 2021-06-02 23:27:17 +09:00
w3c_validator.rb Use frozen string literal in guides/ 2017-08-13 22:04:09 +09:00