2016-10-23 17 views
0

私はHerokuにRailsアプリケーションをデプロイしようとしています。私がheroku run rake db:migrateを実行しようとするまで、それは働いていました。Heroku run rake db:展開しようとするとエラーが発生する

私はこのエラーを取得しています:

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 

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 

そして、ここでは、順番に移行しているpost.rb:

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

移行ファイルとモデルを表示できますか? – Flamine

+0

'users'テーブルを持つ' schema.rb'と 'users'フィールドを持つすべての移行を表示します –

+0

@Flamine Updated –

答えて

0

あなたはusersためadmin役割を追加する前に、モデルadminを作成し、rails destroy model Adminrails g migration DropTableAdminsを使用して、それを削除しました。
ここでは、管理テーブルを削除することに注意してください。そして英雄は理解できません。テーブルadminは破棄する必要があります。
このレコードをマイグレーションから削除して、コメントし、gitを押してからherokuに移動してください。
もう一度heroku run rake db:migrate、それはあなたの問題を解決することができます!