Mysql2 :: Error: Duplicate entry '' for key 'index_admin_users_on_email' Ruby on Rails error

I am trying to run ruby ​​on the rails code that I got from github, and I ended up installing mysql using homebrew and then also installed it directly from http://dev.mysql.com/ . Only then did I use rake in the application file, which began to run tests. However, now I keep getting this Mysql2 :: Error: Duplicate entry, and I don't understand why. Could this be the fact that I have two mysqls located on different computers? I'm not quite sure what causes this problem because all tables are empty. How can I fix this problem? Thanks for the help. I am new to Ruby on Rails and Mysql.

The stack is as follows:

HomeControllerTest test_should_get_index ERROR Mysql2::Error: Duplicate entry '' for key 'index_admin_users_on_email': INSERT INTO `admin_users` (`created_at`, `updated_at`, `id`) VALUES ('2011-12-09 09:33:30', '2011-12-09 09:33:30', 298486374) STDERR: Exception `ActiveRecord::RecordNotUnique' at /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `block in execute' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `execute' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:302:in `insert_fixture' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:486:in `block (5 levels) in create_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:485:in `each' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:485:in `block (4 levels) in create_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:484:in `each' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:484:in `block (3 levels) in create_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:476:in `each' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:476:in `block (2 levels) in create_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:475:in `block in create_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:202:in `disable_referential_integrity' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:460:in `create_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:924:in `load_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.1.3/lib/active_record/fixtures.rb:890:in `setup_fixtures' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:413:in `_run_setup_callbacks' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks' /Users/nick01s/.rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.1.3/lib/active_support/testing/setup_and_teardown.rb:34:in `run' 
+4
source share
2 answers

It is not true that two mysql installations cause this problem. Rails should connect to only one database. You may not know which one is connecting to, but additional work is required to connect it to several db.

I saw this duplicate key error before when the tests in the tests were bad.

In your stack trace, it seems to me that something (fixtures or factories) is trying to create admin_user without a required field. Twice.

The second time it throws an exception because the index is set to unique.

+4
source

For everyone who got this error, it came down to (for me) setting active_admin to an existing project.

The admin_users.yml fixture file was initialized with rails to insert two empty records:

 # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html # This model initially had no columns defined. If you add columns to the # model remove the '{}' from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # one: {} # column: value # two: {} # column: value 

Commenting / deleting these empty inserts fixed the error for me.

+15
source

Source: https://habr.com/ru/post/1385505/


All Articles