为什么会出现重复列错误?

Rhy*_*s0h 4 ruby postgresql activerecord ruby-on-rails

active我正在尝试向我的表添加新列students

我跑去rails g migration add_active_to_students active:boolean生成这个迁移:

class AddActiveToStudents < ActiveRecord::Migration[5.0]
  def change
    add_column :students, :active, :boolean, default: true
  end
end
Run Code Online (Sandbox Code Playgroud)

但是当我运行时rails db:migrate我收到此错误:

PG::DuplicateColumn:错误:关系“学生”的列“活动”已存在:ALTER TABLE“学生”添加“活动”布尔值默认“t”`

正如您所看到的,实际上没有一activestudents

create_table "students", force: :cascade do |t|
    t.integer  "club_id"
    t.string   "email"
    t.string   "address_line_1"
    t.string   "address_line_2"
    t.string   "city"
    t.string   "state"
    t.integer  "postcode"
    t.string   "phone1"
    t.string   "phone2"
    t.string   "first_name"
    t.string   "last_name"
    t.date     "dob"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.string   "picture"
    t.integer  "payment_plan_id"
    t.string   "parent1"
    t.string   "parent2"
    t.string   "size"
    t.text     "notes"
    t.index ["club_id"], name: "index_students_on_club_id", using: :btree
  end
Run Code Online (Sandbox Code Playgroud)

那么为什么我会收到这个错误呢?

Rhy*_*s0h 14

我按照@demir 发布的步骤进行操作,发现该列在数据库中,但没有在架构中列出。ALTER TABLE students DROP COLUMN active没有给出错误消息,但也没有删除该列。

最后我通过以下方式删除了它:

  1. 进入控制台

rails console

  1. 删除列

ActiveRecord::Base.connection.remove_column :students, :active