rake db:migrate 不起作用 [英] rake db:migrate is not working

查看:36
本文介绍了rake db:migrate 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆