Rails Testing - Ruby on Rails Testing - ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint “xxx_xxxx_xxxx_pkey”

0

I have the following Models. I am using Rails 6.0.3.2 (Ruby 2.7.1p83) and using Minitest as Testing Engine

class AuthContentType < ApplicationRecord
  has_many :auth_permissions
end
class AuthGroup < ApplicationRecord
  has_many :auth_group_permissions
  has_many :auth_permissions, through: :auth_group_permissions
end
class AuthGroupPermission < ApplicationRecord
  belongs_to :auth_group
  belongs_to :auth_permission
end
class AuthPermission < ApplicationRecord
  belongs_to :auth_content_type
  has_many :auth_user_permissions
  has_many :users, through: :auth_user_permissions
  has_many :auth_group_permissions
  has_many :auth_groups, through: :auth_group_permissions
end

When I run > rails test test\controllers\XXXX_controller.rb -n test_should_get_home I get the following Error:

Run options: -b -n test_should_get_home --seed 27312                                                                                                                                         
                                                                                                                                                                                             
# Running:                                                                                                                                                                                   
                                                                                                                                                                                             
E                                                                                                                                                                                            
                                                                                                                                                                                             
Error:                                                                                                                                                                                       
GdControllerTest#test_should_get_home:                                                                                                                                                       
ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "auth_content_types_pkey"                                                         
DETAIL:  Key (id)=(980190962) already exists.                                                                                                                                                
                                                                                                                                                                                             
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec'                                         
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `block (2 levels) in execute'                  
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.2/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.0.3.2/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'                                                          
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.2/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'                                                
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:91:in `block in execute'                             
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log'                                   
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.2/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.0.3.2/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.0.3.2/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.0.3.2/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.0.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'                                          
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:721:in `block in log'                                              
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.2/lib/active_support/notifications/instrumenter.rb:24:in `instrument'                                                         
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract_adapter.rb:712:in `log'                                                       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:90:in `execute'                                      
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:170:in `execute_batch'                               
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:370:in `block (3 levels) in insert_fixtures_set'       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `block in transaction'                          
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'                       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.2/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.0.3.2/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.0.3.2/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.0.3.2/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.0.3.2/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'                                          
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'                                
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:280:in `transaction'                                   
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:369:in `block (2 levels) in insert_fixtures_set'       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/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.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:368:in `block in insert_fixtures_set'                  
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:480:in `with_multi_statements'                         
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/connection_adapters/abstract/database_statements.rb:367:in `insert_fixtures_set'                           
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/fixtures.rb:608:in `block in insert'                                                                       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/fixtures.rb:599:in `each'                                                                                  
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/fixtures.rb:599:in `insert'                                                                                
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/fixtures.rb:585:in `read_and_insert'                                                                       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/fixtures.rb:545:in `create_fixtures'                                                                       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/test_fixtures.rb:205:in `load_fixtures'                                                                    
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/test_fixtures.rb:118:in `setup_fixtures'                                                                   
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.2/lib/active_record/test_fixtures.rb:8:in `before_setup'                                                                       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.2/lib/active_support/testing/setup_and_teardown.rb:40:in `before_setup'                                                       
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.2/lib/action_dispatch/testing/integration.rb:322:in `before_setup'                                                               
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activejob-6.0.3.2/lib/active_job/test_helper.rb:45:in `before_setup'                                                                              
    C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-6.0.3.2/lib/rails/test_help.rb:48:in `before_setup'                                                                                      
                                                                                                                                                                                             
                                                                                                                                                                                             
rails test test/controllers/gd_controller_test.rb:4                                                                                                                                          
                                                                                                                                                                                             
.                                                                                                                                                                                            
                                                                                                                                                                                             
Finished in 2.872201s, 0.6963 runs/s, 0.3482 assertions/s.                                                                                                                                   
2 runs, 1 assertions, 0 failures, 1 errors, 0 skips 

Could I be helped on this? Having been trying to perform some solutions suggested. But it doesn’t help me.

Solution:1 (Ran this in the console)

    ActiveRecord::Base.connection.tables.each do |t|
      ActiveRecord::Base.connection.reset_pk_sequence!(t)
    end

Hi @jeydurai, what is the test.

The error is saying most of the things. Record is not unique and it should be. Probably a fixture is creating something?