現在Userモデルに対して以下のようなテストを実行しています。
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "[email protected]")
end
test "should be valid" do
assert @user.valid?
end
test "name should be present" do
@user.name = " "
assert_not @user.valid?
end
end
Userモデル
class User < ApplicationRecord
validates :name, presence: true
end
rails test を実行したところ
duplicate key value violates unique constraint "movies_pkey"
のエラーが出ました。
以下の記事を参考にrails consoleにて
ActiveRecord::Base.connection.tables.each do |t|
ActiveRecord::Base.connection.reset_pk_sequence!(t)
end
を実行したのですが同じエラーコードが出てしまいます。
https://qiita.com/YutoYasunaga/items/a89805441d6961ef658a
どのようにすればエラーを修正できるでしょうか?
エラー全文
Error:
UserTest#test_name_should_be_present:
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "movies_pkey"
DETAIL: Key (id)=(980190962) already exists.
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:47:in `exec'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:47:in `block (2 levels) in execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:46:in `block in execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract_adapter.rb:696:in `block (2 levels) in log'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract_adapter.rb:695:in `block in log'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract_adapter.rb:687:in `log'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:45:in `execute'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:129:in `execute_batch'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:413:in `block (3 levels) in insert_fixtures_set'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `block in transaction'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `transaction'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:412:in `block (2 levels) in insert_fixtures_set'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql/referential_integrity.rb:19:in `disable_referential_integrity'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:411:in `block in insert_fixtures_set'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:523:in `with_multi_statements' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/abstract/database_statements.rb:410:in `insert_fixtures_set'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/fixtures.rb:638:in `block in insert'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/fixtures.rb:629:in `each'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/fixtures.rb:629:in `insert'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/fixtures.rb:615:in `read_and_insert'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/fixtures.rb:567:in `create_fixtures'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/test_fixtures.rb:268:in `load_fixtures'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/test_fixtures.rb:122:in `setup_fixtures'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/test_fixtures.rb:10:in `before_setup'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/testing/setup_and_teardown.rb:40:in `before_setup'
rails test test/models/user_test.rb:13
.
Finished in 0.139483s, 14.3386 runs/s, 7.1693 assertions/s.
2 runs, 1 assertions, 0 failures, 1 errors, 0 skips