Ruby on Rails ActiveRecord :: PendingMigrationError

时间:2018-01-08 02:11:20

标签: ruby-on-rails ruby sqlite

当我尝试在我的localhost上加载我的rails应用程序时,我收到错误,告诉我通过运行rails db:migrate来解决它但是当我尝试运行命令时这就是我得到的

C:\Sites\CoolGuyGear>rails db:migrate

== 20180108004216 AddDeviseToViews: migrating =================================
-- change_table(:views)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: email: ALTER TABLE "views" ADD "email" varchar DEFAULT '' NOT NULL
C:/Sites/CoolGuyGear/db/migrate/20180108004216_add_devise_to_views.rb:7:in `block in up'
C:/Sites/CoolGuyGear/db/migrate/20180108004216_add_devise_to_views.rb:5:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: duplicate column name: email: ALTER TABLE "views" ADD "email" varchar DEFAULT '' NOT NULL
C:/Sites/CoolGuyGear/db/migrate/20180108004216_add_devise_to_views.rb:7:in `block in up'
C:/Sites/CoolGuyGear/db/migrate/20180108004216_add_devise_to_views.rb:5:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'

Caused by:
SQLite3::SQLException: duplicate column name: email
C:/Sites/CoolGuyGear/db/migrate/20180108004216_add_devise_to_views.rb:7:in `block in up'
C:/Sites/CoolGuyGear/db/migrate/20180108004216_add_devise_to_views.rb:5:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

迁移:

# frozen_string_literal: true

class AddDeviseToViews < ActiveRecord::Migration[5.1]
  def self.up
    change_table :views 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.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      # Uncomment below if timestamps were not included in your original model.
      # t.timestamps null: false
    end

    add_index :views, :email,                unique: true
    add_index :views, :reset_password_token, unique: true
    # add_index :views, :confirmation_token,   unique: true
    # add_index :views, :unlock_token,         unique: true
  end

  def self.down
    # By default, we don't want to make any assumption about how to roll  
    #back a migration when your
    # model already existed. Please edit below which fields you would like to remove in this migration.
  raise ActiveRecord::IrreversibleMigration
  end
end


 create_table "views", 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.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_views_on_email", unique: true
    t.index ["reset_password_token"], name: 
"index_views_on_reset_password_token", unique: true
 end

1 个答案:

答案 0 :(得分:3)

问题的答案就在于此。

您添加的任何迁移都会在数据库中创建重复列,如迁移生成的日志中所示。

B

创建一个迁移,从数据库中删除这些列或直接进入本地mysql / sqlite数据库并手动删除这些列。再次运行迁移,因为您的数据库表中还没有这些列,所以不会收到这些错误。