迁移被取消和重复列错误

Migrations are cancelled and duplicate column error

当我写 rails db:migrate 时,我得到这个错误:

bundle exec rake db:migrate

== 20161209073230 AddActivationToUsers: migrating ============================= -- add_column(:users, :activation_digest, :string) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: duplicate column name: activation_digest: ALTER TABLE "users" ADD "activation_digest" varchar

这是我的用户 table:

create_users.rb

class CreateUsers < ActiveRecord::Migration 
  def change
     create_table :users do |t|
     t.string :name
       t.string :email

       t.timestamps null: false
     end 
  end 
end

这是我的 add_activation_to_users.rb:

class AddActivationToUsers < ActiveRecord::Migration
  def change
    add_column :users, :activation_digest, :string
    add_column :users, :activated, :boolean, default: false
    add_column :users, :activated_at, :datetime
  end
end

您的用户 table 中已经有 activation_digest。 请检查您的 schema.rb。如果您尝试添加新列,则需要删除现有列。

SQLite3::SQLException: duplicate column name: activation_digest: ALTER TABLE "users" ADD "activation_digest" varchar

如异常所示,您已经在 users table 中添加了 activation_digest

您可以直接从 rails console 使用 User.column_names 查看 users table 的列。