Skip to content
  • Alex Ghiculescu's avatar
    47467fe3
    Verify foreign keys after loading fixtures · 47467fe3
    Alex Ghiculescu authored
    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](https://github.com/rails/rails/blob/f263530bf709f611920b5f663882e5113b4f984b/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.
    47467fe3
    Verify foreign keys after loading fixtures
    Alex Ghiculescu authored
    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](https://github.com/rails/rails/blob/f263530bf709f611920b5f663882e5113b4f984b/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.
Loading