尝试部署到heroku时出错(跟随ror教程)PG ::错误:错误:列" password_digest"关系"用户"已经存在

时间:2014-08-18 06:27:39

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord heroku

所以我跟随http://www.railstutorial.org/book,并且evrything在本地工作正常(运行sqlight3)。

尝试

时出现以下错误
  

heroku run rake db:migrate

这是错误消息的样子

  

运行rake db:migrate附加到终端...上,运行4049   迁移到AddPasswordDigestToUsers(20140817014655)   == 20140817014655 AddPasswordDigestToUsers:迁移=========================    - add_column(:users,:password_digest,:string)PG :: Error:ERROR:column" password_digest"关系"用户"已存在:ALTER   表"用户"添加栏目" password_digest"字符变化(255)耙   中止! StandardError:发生错误,此错误以及稍后发生   迁移已取消:

     

PG ::错误:错误:列" password_digest"关系"用户"   已存在:ALTER TABLE" users"添加栏目" password_digest"   字符   变(255)/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in   exec' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in 阻止执行'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:442:in block in log' /app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.8/lib/active_support/notifications/instrumenter.rb:20:in instrument'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:437:in log' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in 执行'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:360:in   add_column' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/schema_statements.rb:395:in add_column'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:629:in   {_ 1}}阻止在say_with_time'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in   block in method_missing' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in 的method_missing'   /app/db/migrate/20140817014655_add_password_digest_to_users.rb:3:in   say_with_time' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:621:in exec_migration'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:559:in   change' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:575:in 阻止迁移'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in   block (2 levels) in migrate' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:558:in 迁移'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:713:in   execute_migration_in_transaction'中的with_connection' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:557:in 阻止   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in   交易中migrate' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:963:in 阻止'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in   block in ddl_transaction' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in 交易'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:209:in within_new_transaction' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in ddl_transaction'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:962:in   transaction' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in 阻止迁移'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in   execute_migration_in_transaction' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:924:in 迁移'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:768:in   each' /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in 迁移'   /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:42:in   `块(2级)'任务:TOP => db:migrate(参见   使用--trace运行任务完全跟踪

我已经尝试过heroku pg:重置DATABASE_URL然后重试。 还尝试将/ spec,/ lib,/ script,/ features,/ cucumber.yml添加到.slugignore

这就是我的架构:

up'
  /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:746:in
  

这就是我的密码摘要迁移看起来像

ActiveRecord::Schema.define(version: 20140818041701) do

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "password_digest"
    t.string   "remember_token"
    t.boolean  "admin",           default: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["remember_token"], name: "index_users_on_remember_token"

end

2 个答案:

答案 0 :(得分:0)

您的表格中已经有password_digest列,而迁移AddPasswordDigestToUsers您正在尝试创建另一个列,这就是创建问题的原因。删除迁移AddPasswordDigestToUsers,然后再次尝试运行rake db:migrate。在这种情况下,Sqlite3不会产生任何错误。如果您在迁移中编写intege而不是integer,则在使用sqlite时不会产生任何错误。

答案 1 :(得分:0)

如果您只是删除密码摘要迁移文件,它将解决部署到heroku的短期问题,但随后协作者无法在本地下拉和设置其数据库。它们会遇到问题并可能会创建另一个迁移文件,从而产生同样的问题。

我不确定最佳解决方案,但到目前为止这似乎对我有用:

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    if Rails.env == "production"
    else
      add_column :users, :password_digest, :string   
    end
  end
end