我正在学习 rails 教程,但被卡住了.从代码清单 8.16 开始,我对 _add_remember_token_to_users.rb 做了以下修改:
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:
class AddRememberTokenToUsers < ActiveRecord::Migration def change add_column :users, :remember_token, :string add_index :users, :remember_token end end然后指南说要更新 dev &照常测试数据库:
The guide then says to update dev & test db as usual:
$ bundle exec rake db:migrate $ bundle exec rake db:test:prepare我对 *remember_token* 的用户测试仍然失败,所以我使用命令行 sqlite3 查看了 dev 和 tests 数据库中的 user 表.它们看起来像这样:
My User test for the *remember_token* is still failing so I took a look at the user table in dev and tests database with command line sqlite3. They look like this:
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.
推荐答案尝试重建你的数据库结构(警告:所有的数据库数据都将丢失):
Try to rebuild your database structure(WARNING: all db-data will be lost):
rake db:drop:all rake db:create:all rake db:migrate如果你使用 Rails <4.1、不要忘记准备测试数据库:
If you use Rails < 4.1, don't forget to prepare test database:
rake db:test:prepare这是最简单的解决方案,因为您正在使用教程.但是,在生产中或在开发中有重要数据时,您应该花时间调查问题.在这种情况下,您很可能创建了一个空迁移,运行了 rake db:migrate,然后向迁移添加了说明,因此您看不到新字段和进一步的 rake db:migrate 什么都不做.要解决此问题,您需要注释 change 指令,执行 rake db:rollback,取消注释指令,然后 rake db:migrate 以应用指令你错过了.
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:migrate does nothing. To resolve this issue you need to comment your change instructions, perform rake db:rollback, uncomment instructions and then rake db:migrate to apply instructions you missed.
更多推荐
rake db:migrate 不起作用
发布评论