我正在关注Lynda ruby on rails教程。我已经运行了代码" rake db:migrate"经过几次不成功的尝试。然后我被告知要运行" rake db:migrate VERSION = 0"。这再一次给了我错误,这次我无法修复它。我昨晚和今天早上都试过无济于事。
请记住,在回答我是新手编码时,请以新手可以理解的方式解释。
lexi87$ rake db:migrate VERSION=0
== AlterUsers: reverting =====================================================
-- remove_index("admin_users", "username")
rake aborted!
An error has occurred, all later migrations canceled:
Index name 'index_admin_users_on_username' on table 'admin_users' does not exist
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/schema_statements.rb:587:in `index_name_for_remove'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/schema_statements.rb:366:in `remove_index'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:466:in `block in method_missing'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:438:in `say_with_time'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:458:in `method_missing'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:334:in `method_missing'
/Users/lexi87/Sites/simple_cms/db/migrate/20130108015542_alter_users.rb:14:in `down'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:376:in `down'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:410:in `block in migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:389:in `migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:528:in `migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:777:in `call'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:777:in `ddl_transaction'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:719:in `block in migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:700:in `each'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:700:in `migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:574:in `down'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/migration.rb:555:in `migrate'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.9/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/bin/ruby_noexec_wrapper:14:in `eval'
/Users/lexi87/.rvm/gems/ruby-1.9.2-p320/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
这是rb文件。
class AlterUsers < ActiveRecord::Migration
def self.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 an index ***"
add_index("admin_users", "username")
end
def self.down
remove_index("admin_users", "username")
remove_column("admin_users", "salt")
remove_column("admin_users", "hashed_password", "password")
remove_column("admin_users", "email", :string, :default => "", :null => false)
remove_column("admin_users", "username")
rename_table("admin_users", "users")
end
end
答案 0 :(得分:4)
rake db:migrate VERSION = 0 回滚所有迁移
错误明确表示
Index name 'index_admin_users_on_username' on table 'admin_users' does not exist
我怀疑你实际上是在尝试回滚从未应用的迁移,因此索引不存在。 或者,您已在应用迁移后对其进行更改。 我的建议是 删除数据库,再次创建并应用所有迁移。
rake db:drop
rake db:create
rake db:migrate
答案 1 :(得分:1)
您正在删除不存在的索引。始终使用以下顺序:
1)rake db:create
2)rake db:migrate
此外,在您收到的错误的上下文中,请记住rake db:migrate VERSION=0
将删除所有迁移,如果这是您正在尝试执行的操作,并且因为之前没有索引,那么它将通过错误。
我还怀疑包含mysql设置的database.yml
文件的凭据错误,尤其是username
和password
的设置。检查并更正它,然后进行迁移。
答案 2 :(得分:0)
猜 1.你不能跳回到其他版本(它总是在终端上显示错误)。 你也不能耙db:rollback&#39; 你不能耙db:向下迁移&#39;同样。 你正在使用MySQL。
深吸一口气,尝试这样做。
一个。打开终端应用程序,然后输入。
rake db:drop
之后,尝试查看您的数据库。必须没有&#34;您的数据库&#34;。
B中。重新创建数据库(直接使用MySQL命令或跟进Rake命令) 对于MySQL
CREATE DATABASE database_name;
for Rake
rake db:create
尤金先生回答说。
℃。进行迁移,转到终端并输入。
rake db:migrate
之后,您应该登录到您的数据库以检查其上的任何活动。