问题
I recently added devise confirmable module to my rails app. My application runs well in dev. But, when I push it to heroku, it says "We're sorry, but something went wrong.". Following is my heroku log-
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 #<User:0x000000023254e0>):
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]:
It is clear that the database does not have "confirmed_at" column. My migration file is as follows:
class AddConfirmableToDeviseV1 < ActiveRecord::Migration
def up
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at , :datetime
add_index :users, :confirmation_token, :unique => 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
My production is hosted on heroku:
So I ran
heroku run rake db:migrate
It gave me no output.
Then I ran
heroku run rake db:rollback
It gave me
== 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
Also,
heroku run rake db:migrate --trace
gives me following
** 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
Any Ideas how to solve this issue?
回答1:
You will definitely need to make sure your migrations have been run against the production database.
RAILS_ENV=production rake db:migrate
This presumes that your local environment can access the production database of course.
(Note: this is a generic comment - not a heroku specific answer - "heroku run rake db:migrate" is apparently the correct way to run migrations against production Heroku environment)
回答2:
Problem solved.
I deleted my migration files. Added a new migration file with devise helper in migration change method.
class AddConfirmableToDevise < ActiveRecord::Migration
def change
change_table(:users) do |t|
t.confirmable
end
add_index :users, :confirmation_token, :unique => true
end
end
Ran the migration again with
heroku run rake db:migrate
And whoa...it worked. I do not know how does this make sense, but I am glad that it worked. Please leave comments, I am keen to read explanation.
Anyway, thanks everybody.
来源:https://stackoverflow.com/questions/9122786/heroku-rails-app-database-migration-issues