Rails MInitestで「Foreign key violations found in your fixture data.」エラー

現象

rails test を実行すると以下のエラーが発生。

% rails test
Running 1 tests in a single process (parallelization threshold is 50)
Run options: --seed 51094

# Running:

E

Error:
UsersControllerTest#test_should_get_index:
RuntimeError: Foreign key violations found in your fixture data. Ensure you aren't referring to labels that don't exist on associations.
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/fixtures.rb:633:in `block in insert'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/fixtures.rb:621:in `each'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/fixtures.rb:621:in `insert'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/fixtures.rb:607:in `read_and_insert'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/fixtures.rb:567:in `create_fixtures'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/test_fixtures.rb:271:in `load_fixtures'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/test_fixtures.rb:125:in `setup_fixtures'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3/lib/active_record/test_fixtures.rb:10:in `before_setup'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3/lib/active_support/testing/setup_and_teardown.rb:40:in `before_setup'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3/lib/action_dispatch/testing/integration.rb:331:in `before_setup'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/activejob-7.0.3/lib/active_job/test_helper.rb:48:in `before_setup'
    /rails-api/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3/lib/rails/test_help.rb:48:in `before_setup'


rails test test/controllers/users_controller_test.rb:8



Finished in 0.193975s, 5.1553 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

原因

fixturesが外部キーを正しく登録できていないため、アソシエーションエラーになっている。
db/schema.rb で指定されたとおりのアソシエーションのデータが登録されていないと、このようなエラーになる。

対応

今回は外部キーを張るだけでよかったので、例として以下のように対応。

test/fixtures/users.yml

one: ← これが定義名
  wallet_address: "HXtBm8XZbxaTt41uqaKhwUAa6Z1aPyvJdsZVENiWsetg"

test/fixtures/scan_histories.yml

one:
  user: one ← 「モデル名:定義名」で紐付けできる
  barcode: "45019517"

(参考)app/models/user.rb

class User < ApplicationRecord
  has_many :scan_histories
end

(参考)app/models/scan_history.rb

class ScanHistory < ApplicationRecord
  belongs_to :user
end

(参考)db/schema.rb

  create_table "users", force: :cascade do |t|
    t.text "wallet_address"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["wallet_address"], name: "index_users_on_wallet_address", unique: true
  end

  create_table "scan_histories", force: :cascade do |t|
    t.integer "user_id", null: false
    t.text "barcode"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_scan_histories_on_user_id"
  end