繁体   English   中英

尝试部署时,Heroku运行rake db:migrate错误

[英]Heroku run rake db:migrate error when trying to deploy

我正在尝试在Heroku上部署我的Rails应用程序。 在我尝试运行heroku run rake db:migrate之前,它一直在工作。

我收到此错误:

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedColumn: ERROR:  column "admin" of relation "users" does not exist
: ALTER TABLE "users" DROP "admin"

这是我的架构:

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

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "comments", force: :cascade do |t|
  t.text     "body"
  t.integer  "post_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer  "user_id"
  t.index ["post_id"], name: "index_comments_on_post_id", using: :btree
end

create_table "posts", force: :cascade do |t|
  t.string   "title"
  t.text     "body"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
  t.string   "email",                  default: "",    null: false
  t.string   "encrypted_password",     default: "",    null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,     null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.inet     "current_sign_in_ip"
  t.inet     "last_sign_in_ip"
  t.datetime "created_at",                             null: false
  t.datetime "updated_at",                             null: false
  t.string   "first_name"
  t.string   "last_name"
  t.boolean  "admin",                  default: false
  t.integer  "admin_id"
  t.index ["admin_id"], name: "index_users_on_admin_id", using: :btree
  t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
  t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end

  add_foreign_key "comments", "posts"
end

这是我的模型:

comment.rb

class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :user

  validates :body, presence: true

end

post.rb

class Post < ApplicationRecord
  has_many :comments, dependent: :destroy
  validates :title, presence: true, length: { minimum: 5 }
  validates :body, presence: true

  def self.search(search)
    where("title LIKE ? OR body LIKE ?", "%#{search}%", "%#{search}%")
  end
end

user.rb

class User < ApplicationRecord
  has_many :comments, dependent: :destroy

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  def full_name
    "#{first_name} #{last_name}"
  end

end

以下是迁移顺序:

class CreatePosts < ActiveRecord::Migration[5.0]
  def change
    create_table :posts do |t|
      t.string :title
      t.text :body

      t.timestamps
    end
  end
end

class CreateComments < ActiveRecord::Migration[5.0]
  def change
    create_table :comments do |t|
      t.string :name
      t.text :body
      t.references :post, foreign_key: true

      t.timestamps
    end
  end
end

class DeviseCreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.inet     :current_sign_in_ip
      t.inet     :last_sign_in_ip

      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
  end
end

class AddFirstAndLastNameToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
  end
end

class RemoveNameFromComments < ActiveRecord::Migration[5.0]
  def change
    remove_column :comments, :name
  end
end

class AddUserIdToComments < ActiveRecord::Migration[5.0]
  def change
    add_column :comments, :user_id, :integer
  end
end

class AddAdminToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :admin, :boolean, default: false
  end
end

class AddRefToUsers < ActiveRecord::Migration[5.0]
  def change
    add_reference :users, :admin, index: true
  end
end

class DropTableAdmins < ActiveRecord::Migration[5.0]
  def change
    drop_table :admins
  end
end

我已经尝试修复了几个小时。 任何帮助将不胜感激!

在为users添加admin角色之前,您使用rails destroy model Adminrails g migration DropTableAdmins创建了模型admin ,然后将其删除。
现在,您仅需注意有关删除admins表的信息,而heroku不了解,必须删除表admin地方。
只需从迁移中删除此记录,注释并推送到git,然后推送到heroku。
再次heroku run rake db:migrate ,它可以解决您的问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM