add_index migration fails with "Argument out of range"
I'm trying to duplicate a (working!) Rails 3/MySql application on another computer. I created a schema.rb and data.yml, then used db:schema:load and db:data:load, and it all worked perfectly except for one thing. I had a number of indexes on my tables, and rake fails trying to create them--I had to comment them out of schema.rb to get it to generate, so I tried putting them into a migration (the way they were created in the original app!) and rake still died. Here's the error message:
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开发者_C百科/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>'
Here is the offending migration:
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
This is exactly the same code I used to create the indexes in the original app. As far as I can tell, I'm running exactly the same environment on this computer-- Ruby 1.9.2, Rails 3.0.3, mysql2 0.2.6 x86-mingw32, MySQL 5.1.41. I even copied over the same libmysql.dll. I'm stumped.
EDIT
Haha, I'm stupid. I was updating the libmysql.dll in the MySQL\MySQL Server 5.1\bin but not the one in Ruby\bin. Once I copied the latest version into Ruby\bin, everything worked fine. Thanks, everyone!
It seems like a lot of people have this problem, me included. The error seems to originate from the mysql2 gem. So what worked for me was:
- Install mysql gem
- Change from mysql2 to mysql in Gemfile
- Change adapter from mysql2 to mysql in database.yml
And then possible drop the last table created if it were created in the same migration as the call to add_index since it will try to create it once again.
Perhaps more of a workaround then a solution but I have not encountered any better way yet.
精彩评论