0
0

テーブルのレコードをコピペする

Last updated at Posted at 2024-07-16

rspecを実行時のエラー

1) User with 2 or more comments orders them in reverse chronologically
     Failure/Error: user = User.create!(:name => "hogehoge", :email => "gehogeho", :password => "gehogeho", :forign => 1)
     
     ActiveRecord::InvalidForeignKey:
       Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`test`.`users`, CONSTRAINT `fk_rails_8f8a4b3fcb` FOREIGN KEY (`forign`) REFERENCES `forign` (`id`))
     # ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # Mysql2::Error:
     #   Cannot add or update a child row: a foreign key constraint fails (`test`.`users`, CONSTRAINT `fk_rails_8f8a4b3fcb` FOREIGN KEY (`forign`) REFERENCES `forign` (`id`))
     #   ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.02786 seconds (files took 0.90184 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:5 # User with 2 or more comments orders them in reverse chronologically

どうやらテスト環境のデータベースにforignのidがないらしい

レコードだけをコピーする

mysql> insert into test.forign select * from development.forign
    -> ;

エラー文が別の外部キーにidがないと言われたので成功したのだろう

出典

感想

テストについて知らなかったのでこれから取り組む

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0