我正在尝试在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
我一直试图解决这个问题几个小时。任何帮助将不胜感激!
答案 0 :(得分:0)
在为admin
添加users
角色之前,您已使用admin
和rails destroy model Admin
创建了模型rails g migration DropTableAdmins
,然后将其删除。
现在,你只注意到丢弃管理员表,而heroku不明白,表admin
在哪里,必须销毁。
只需从迁移中删除此记录,注释并推送到git然后再到heroku
再次heroku run rake db:migrate
,它可以解决您的问题!