ruby-on-rails - Ruby on Rails ActiveRecord::PendingMigrationError

标签 ruby-on-rails ruby sqlite

当我尝试在我的本地主机上加载我的 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

最佳答案

答案在问题中。

无论您添加了什么迁移,都会在数据库中创建重复的列,如迁移生成的日志所示。

重复的列名:email: ALTER TABLE "views"ADD "email"

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

关于ruby-on-rails - Ruby on Rails ActiveRecord::PendingMigrationError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143433/

相关文章:

sql - 指向多个表之一的外键约束

sqlite - 使用FireDAC读取 'bad' Sqlite数据

ruby-on-rails - Rails3 : how to set default conditions to has_many

ruby-on-rails - Rails Devise Gem,(未定义方法 `to_datetime' for nil :NilClass) when i want to recover a password

ruby - 使用 ruby​​ mechanize 保存文件

ruby - 如何拆分数组?

ruby - 如何使用 Rake 为 Rspec 运行 JunitFormatter 输出?

sql - 在 sqlite3 中删除级联

ruby-on-rails - 更新后如何保持 Gemfile 中的所有 gem 兼容

ruby-on-rails - 使用 Rails 6 实现 Google Adsense