0

大家好,我是 ruby​​ on rails 的新手,所以我用 rake db:migrate 遇到了这个问题,所以我使用 --trace up 来理解它,但我不知道该怎么做。我把我的迁移 .rb 文件放在下面。

 $ rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
==  AlterUsers: migrating =====================================================
-- rename_table("users", "admin_users")
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'admin_users' already exists: RENAME TABLE `users` TO `admin_users`
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `log'
/Library/Ruby/Gems/1.8/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:457:in `rename_table'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:466:in `send'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:466:in `method_missing'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `say_with_time'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `say_with_time'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:458:in `method_missing'
/Users/seydoukonate/Sites/simple_cms/db/migrate/20121015072622_alter_users.rb:3:in `up'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:410:in `send'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:410:in `migrate'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:410:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in `with_connection'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:389:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:528:in `__send__'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:528:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:720:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `call'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `ddl_transaction'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:719:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `each'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:570:in `up'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/migration.rb:551:in `migrate'
/Library/Ruby/Gems/1.8/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:153
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.9.2.2/bin/rake:33
/usr/bin/rake:23:in `load'
/usr/bin/rake:23
Tasks: TOP => db:migrate

这是我的 _create_user.rb 文件中的内容。

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|

      t.string "first_name", :limit => 25
      t.string "last_name", :limit => 50
      t.string "email", :default => "", :null => false
      t.string "password", :limit => 40
      t.timestamps
    end
  end
  def self.down
    drop_table :users
  end
end

这是 alter_user.rb 文件中的内容

class AlterUsers < ActiveRecord::Migration
  def up
    rename_table("users", "admin_users")
    add_column("admin_users", "username", :string, :limit => 25)
    change_column("admin_users", "email", :string, :limit => 100)
    rename_column("admin_users", "password", "hashed_password")
    add_column("admin_users", "salt", :string, :limit => 40)
    puts "*** About to add and index ***"
    add_index("admin_users", "username")
  end

  def down
    remove_index("admin_users", "username")
    remove_column("admin_users", "salt")
    rename_column("admin_users", "hashed_apssword", "password")
    change_column("admin_users", "email", :string, :default => "", :null => false)
    remove_column("admin_users", "username")
    rename_table("admin_users", "users")
  end
end
4

3 回答 3

1

错误信息很清楚

Mysql2::Error: Table 'admin_users' already exists: RENAME TABLE `users` TO `admin_users`

这意味着该表已经存在。可能是以前的迁移已经创建了它。

于 2012-10-15T08:50:20.740 回答
0

您可能已经安装了设计 gem,它已经创建了一个名为admin_users.

您要么需要admin_users在运行之前删除表(和相应的迁移文件)alter;或者您可以重命名usersadmin_users

于 2012-10-15T08:53:08.377 回答
0

对于本教程,我的意思是(Ruby on Rails 3 Essential Training)如果您遇到上述问题,请尝试执行

rake db:migrate VERSION=0

并运行

rake db:migrate
于 2012-12-27T00:25:55.827 回答