heroku rails app数据库迁移问题
我最近在我的rails应用程序中添加了设计确认模块。 我的应用程序在dev中运行良好。 但是,当我把它推到heroku时,它说“我们很抱歉,但出了点问题。” 以下是我的heroku日志 –
Rendered devise/confirmations/new.html.erb within layouts/application (2.7ms) 2012-02-03T01:55:07+00:00 app[web.1]: Rendered devise/shared/_links.erb (0.5ms) 2012-02-03T01:55:07+00:00 app[web.1]: cache: [GET /users/confirmation/new] miss 2012-02-03T01:55:07+00:00 app[web.1]: Completed 200 OK in 19ms (Views: 16.9ms | ActiveRecord: 0.0ms) 2012-02-03T01:55:07+00:00 heroku[router]: GET personaldiary.herokuapp.com/users/confirmation/new dyno=web.1 queue=0 wait=0ms service=42ms status=200 bytes=1540 2012-02-03T01:55:11+00:00 app[web.1]: 2012-02-03T01:55:11+00:00 app[web.1]: 2012-02-03T01:55:11+00:00 app[web.1]: Started POST "/users/confirmation" for 50.131.164.83 at 2012-02-03 01:55:11 +0000 2012-02-03T01:55:11+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"iVVTpWgIrBTR8b9k07lr2tYDFQfAYD0R8JmGVkmfzl4=", "user"=>{"email"=>"hrishikeshp19@gmail.com"}, "commit"=>"Resend confirmation instructions"} 2012-02-03T01:55:11+00:00 app[web.1]: Processing by Devise::ConfirmationsController#create as HTML 2012-02-03T01:55:11+00:00 app[web.1]: Completed 500 Internal Server Error in 4ms 2012-02-03T01:55:11+00:00 app[web.1]: 2012-02-03T01:55:11+00:00 app[web.1]: NameError (undefined local variable or method `confirmed_at' for #): 2012-02-03T01:55:11+00:00 app[web.1]: 2012-02-03T01:55:11+00:00 app[web.1]: cache: [POST /users/confirmation] invalidate, pass 2012-02-03T01:55:11+00:00 app[web.1]:
很明显,数据库没有“confirmed_at”列。 我的迁移文件如下:
class AddConfirmableToDeviseV1 true end def down remove_index :users, :confirmation_token remove_column :users, :confirmation_sent_at remove_column :users, :confirmed_at remove_column :users, :confirmation_token end end
我的作品在heroku上托管:
所以我跑了
heroku run rake db:migrate
它没有给我输出。
然后我跑了
heroku run rake db:rollback
它给了我
== AddConfirmableToDeviseV1: reverting ======================================= -- remove_index(:users, :confirmation_token) rake aborted! An error has occurred, this and all later migrations canceled: Index name 'index_users_on_confirmation_token' on table 'users' does not exist Tasks: TOP => db:rollback
也,
heroku run rake db:migrate --trace
给了我以下
** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Invoke db:load_config (first_time) ** Invoke rails_env (first_time) ** Execute rails_env ** Execute db:load_config ** Execute db:migrate ** Invoke db:schema:dump (first_time) ** Invoke environment ** Invoke db:load_config ** Execute db:schema:dump
任何想法如何解决这个问题?
您肯定需要确保已针对生产数据库运行迁移。
RAILS_ENV=production rake db:migrate
这假设您的本地环境当然可以访问生产数据库。
(注意:这是一个通用的注释 – 不是heroku特定的答案 – “heroku run rake db:migrate”显然是针对生产Heroku环境运行迁移的正确方法)
问题解决了。
我删除了我的迁移文件。 在迁移更改方法中添加了带有设计帮助程序的新迁移文件。
class AddConfirmableToDevise < ActiveRecord::Migration def change change_table(:users) do |t| t.confirmable end add_index :users, :confirmation_token, :unique => true end end
再次迁移迁移
heroku run rake db:migrate
哇……它有效。 我不知道这有什么意义,但我很高兴它有效。 请留下评论,我热衷于阅读解释。
无论如何,谢谢大家。