Ruby-on-rails rake db:migrate 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9896488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rake db:migrate is not working
提问by hippeelee
I'm working through the rails tutorial and have gotten stuck. Starting at Listing 8.16 I have made the following modifications to <timestamp>_add_remember_token_to_users.rb:
我正在完成 Rails 教程,但遇到了困难。从代码清单 8.16 开始,我对 进行了以下修改<timestamp>_add_remember_token_to_users.rb:
class AddRememberTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :remember_token, :string
add_index :users, :remember_token
end
end
The guide then says to update dev & test db as usual:
然后指南说要像往常一样更新开发和测试数据库:
$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare
My User test for the *remember_token* is still failing so I took a look at the usertable in dev and tests database with command line sqlite3. They look like this:
我对 *remember_token* 的用户测试仍然失败,所以我使用命令行 sqlite3查看了dev 和 tests 数据库中的用户表。它们看起来像这样:
sqlite> .schema users
CREATE TABLE "users" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" varchar(255),
"email" varchar(255),
"created_at" datetime NOT NULL,
"updated_at" datetime NOT NULL,
"password_digest" varchar(255));
CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email");
It seems like my migration has not been run yet but I do not know how to force it to run.
似乎我的迁移尚未运行,但我不知道如何强制它运行。
回答by Rustam A. Gasanov
Try to rebuild your database structure(WARNING:all db-data will be lost):
尝试重建您的数据库结构(警告:所有 db-data 都将丢失):
rake db:drop:all
rake db:create:all
rake db:migrate
If you use Rails < 4.1, don't forget to prepare test database:
如果您使用 Rails < 4.1,请不要忘记准备测试数据库:
rake db:test:prepare
This is the easiest solution since you are working with tutorial. However in production or having important data in development you should take time to investigate the issue. In this case you most likely had created an empty migration, ran rake db:migrate, then added instructions to the migration, so you don't see a new field and further rake db:migratedoes nothing. To resolve this issue you need to comment your changeinstructions, perform rake db:rollback, uncomment instructions and then rake db:migrateto apply instructions you missed.
这是最简单的解决方案,因为您正在使用教程。但是,在生产中或在开发中有重要数据时,您应该花时间调查问题。在这种情况下,您很可能创建了一个空的迁移,rake db:migrate然后运行了迁移,然后向迁移添加了说明,因此您看不到新字段并且rake db:migrate什么也不做。要解决此问题,您需要注释change说明、执行rake db:rollback、取消注释说明,然后rake db:migrate应用您错过的说明。
回答by Livi17
I had the same issue as the initial question. $ bundle exec rake db:migratewasn't adding remember_token to the .db and Latha Doddikadi's answer worked for me.
我和最初的问题有同样的问题。$ bundle exec rake db:migrate没有将 remember_token 添加到 .db 中,Latha Doddikadi 的回答对我有用。
I did:
我做了:
rake db:rollback
and then:
进而:
$ bundle exec rake db:migrate
which added the remember_token field to the database followed by:
其中将 remember_token 字段添加到数据库中,然后是:
bundle exec rspec spec/models/user_spec.rb
which passed.
通过了。
Finished in 0.92841 seconds
21 examples, 0 failures
回答by Maddy
Roll back and then re run the migration it might work.
回滚然后重新运行它可能工作的迁移。
rake db:rollback
And after rolling back re run your migrations again.
并在回滚后再次运行您的迁移。

