add_index迁移失败并显示“Argument out of range”

时间:2011-01-15 01:05:38

标签: mysql ruby-on-rails rake

我正在尝试在另一台计算机上复制(工作!)Rails 3 / MySql应用程序。我创建了一个schema.rb和data.yml,然后使用db:schema:load和db:data:load,除了一件事以外它都完美无缺。我的表上有很多索引,并且rake尝试创建它们失败了 - 我不得不将它们从schema.rb中注释掉来生成它,所以我尝试将它们放入迁移(它们的创建方式)原来的应用程序!)和耙子仍然死了。这是错误消息:

O:\Development\myapp>rake db:migrate
rake aborted!
An error has occurred, all later migrations canceled:

argument out of range

C:/Ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/active_record/connection_adapters/mysql2_adapter.rb:446:in `utc'
C:/Ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/active_record/connection_adapters/mysql2_adapter.rb:446:in `each'
C:/Ruby/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x86-mingw32/lib/active_record/connection_adapters/mysql2_adapter.rb:446:in `indexes'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:406:in `index_name_exists?'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:343:in `add_index'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:383:in `block in method_missing'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:359:in `block in say_with_time'
C:/Ruby/lib/ruby/1.9.1/benchmark.rb:294:in `measure'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:359:in `say_with_time'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:379:in `method_missing'O:/Development/codenotes/db/migrate/20110114212538_add_notes_index.rb:11:in `up'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:312:in `block in migrate'
C:/Ruby/lib/ruby/1.9.1/benchmark.rb:294:in `measure'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:312:in `migrate'
C:in `migrate'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:537:in `block (2 levels) in migrate'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:613:in `call'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:613:in `ddl_transaction'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:536:in `block in migrate'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:523:in `each'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:523:in `migrate'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:433:in `up'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/migration.rb:415:in `migrate'
C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.3/lib/active_record/railties/databases.rake:142:in `block (2 levels) in <top (required)>'
C:/Ruby/lib/ruby/1.9.1/rake.rb:634:in `call'
C:/Ruby/lib/ruby/1.9.1/rake.rb:634:in `block in execute'
C:/Ruby/lib/ruby/1.9.1/rake.rb:629:in `each'
C:/Ruby/lib/ruby/1.9.1/rake.rb:629:in `execute'
C:/Ruby/lib/ruby/1.9.1/rake.rb:595:in `block in invoke_with_call_chain'
C:/Ruby/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
C:/Ruby/lib/ruby/1.9.1/rake.rb:588:in `invoke_with_call_chain'
C:/Ruby/lib/ruby/1.9.1/rake.rb:581:in `invoke'
C:/Ruby/lib/ruby/1.9.1/rake.rb:2041:in `invoke_task'
C:/Ruby/lib/ruby/1.9.1/rake.rb:2019:in `block (2 levels) in top_level'
C:/Ruby/lib/ruby/1.9.1/rake.rb:2019:in `each'
C:/Ruby/lib/ruby/1.9.1/rake.rb:2019:in `block in top_level'
C:/Ruby/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
C:/Ruby/lib/ruby/1.9.1/rake.rb:2013:in `top_level'
C:/Ruby/lib/ruby/1.9.1/rake.rb:1992:in `run'
C:/Ruby/bin/rake:31:in `<main>'

以下是违规迁移:

class AddNotesIndex < ActiveRecord::Migration
  def self.up
        add_index :users, :email,                :unique => true
        add_index :users, :reset_password_token, :unique => true
        add_index :users, :confirmation_token,   :unique => true
        add_index :users, :unlock_token,         :unique => true
  end

  def self.down
        remove_index :users, :email,                :unique => true
        remove_index :users, :reset_password_token, :unique => true
        remove_index :users, :confirmation_token,   :unique => true
        remove_index :users, :unlock_token,         :unique => true
  end
end

这与我在原始应用中用于创建索引的代码完全相同。据我所知,我在这台计算机上运行完全相同的环境 - Ruby 1.9.2,Rails 3.0.3,mysql2 0.2.6 x86-mingw32,MySQL 5.1.41。我甚至复制了相同的libmysql.dll。我很难过。

修改

哈哈,我很蠢。我正在更新MySQL \ MySQL Server 5.1 \ bin中的libmysql.dll,但不更新Ruby \ bin中的libmysql.dll。一旦我将最新版本复制到Ruby \ bin中,一切正常。谢谢大家!

1 个答案:

答案 0 :(得分:1)

似乎很多人都有这个问题,包括我在内。该错误似乎源自mysql2 gem。那对我有用的是:

  1. 安装mysql gem
  2. 从Gemfile中的mysql2更改为mysql
  3. 将适配器从mysql2更改为database.yml中的mysql
  4. 然后可能删除创建的最后一个表,如果它是在与add_index的调用相同的迁移中创建的,因为它将尝试再次创建它。

    也许更多的解决方法是解决方案,但我还没有遇到任何更好的方法。