add_index 迁移因“参数超出范围”而失败

发布于 2024-10-11 19:42:48 字数 4751 浏览 5 评论 0原文

我正在尝试在另一台计算机上复制(工作!)Rails 3/MySql 应用程序。我创建了 schema.rb 和 data.yml,然后使用 db:schema:load 和 db:data:load,除了一件事之外,一切都运行良好。我的表上有很多索引,rake 尝试创建它们失败了——我必须将它们从 schema.rb 中注释掉才能生成它,所以我尝试将它们放入迁移中(它们在原来的应用程序!)并且 rake 仍然死了。这是错误消息:

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 中后,一切正常。谢谢大家!

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/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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2024-10-18 19:42:48

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

  1. 安装 mysql gem
  2. 在 Gemfile 中从 mysql2 更改为 mysql
  3. 在 database.yml 中将适配器从 mysql2 更改为 mysql

然后可能会删除创建的最后一个表,如果它是在与调用 add_index 相同的迁移中创建的,因为它将尝试再次创建它。

也许更多的是一种解决方法,而不是解决方案,但我还没有遇到更好的方法。

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:

  1. Install mysql gem
  2. Change from mysql2 to mysql in Gemfile
  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文