2011-12-09 51 views
4

我想從github獲得的rails代碼上運行ruby,最後我安裝了使用自制軟件的mysql,然後最終安裝了它直接從http://dev.mysql.com/。只有當我在應用程序文件中使用rake時,測試纔開始運行。但是,現在我不斷收到這個Mysql2 :: Error:Duplicate條目,我不明白爲什麼。難道我的電腦上有兩個不同的地方有兩個mysqls?我不確定是什麼導致這個問題,因爲所有的表都是空的。我該如何解決這個問題?感謝您的幫助。我是Ruby on Rails和Mysql的新手。Mysql2 ::錯誤:關鍵'index_admin_users_on_email'重複條目''Ruby on Rails錯誤

堆棧如下所示:

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

這是不太可能的mysql的兩個安裝導致此問題。 Rails應該只連接到一個數據庫。你可能不知道它連接到哪一個,但它需要額外的工作才能連接到多個數據庫。

在測試中的燈具壞了之前,我已經看到重複鍵錯誤。

在你的堆棧跟蹤中,它看起來像我(夾具或工廠)試圖創建一個沒有必填字段的admin_user。兩次。

由於索引設置爲唯一,所以第二次拋出異常。

+0

謝謝,你知道我在哪裏可以找到解決這個問題的地方在Ruby on Rails框架?例如,什麼文件會引發這個異常? – Ectac

+0

你的堆棧跟蹤中有一條有趣的行是: lib/active_record/connection_adapters/abstract/database_statements.rb:302:在insert_fixture中所以我首先看夾具文件,如果有工廠查看工廠是否與現有對象存在衝突 – edk750

+0

也查看代碼爲什麼將不完整記錄插入到數據庫中:INSERT INTO'admin_users'('created_at','updated_at','id')VALUES('2011 -12-09 09:33:30','2011-12-09 09:33:30',298486374)...你在這裏錯過了一個電子郵件字段。 – edk750

15

對於其他人得到這個錯誤,這(下)(安裝active_admin到現有的項目)。

的admin_users.yml夾具文件由軌道初始化插入兩個空記錄:

# 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 

註釋掉/刪除這些空插入固定的錯誤我。

+0

不錯,只是評論這兩行「一二」,測試應該回來工作。 –