【发布时间】:2019-09-10 21:35:20
【问题描述】:
我正在尝试将新列 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
但是当我运行rails db:migrate 时,我得到了这个错误:
PG::DuplicateColumn:错误:关系“学生”的“活动”列已存在 : ALTER TABLE "students" ADD "active" boolean DEFAULT 't'`
如您所见,students 中实际上没有 active 列:
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
那么为什么会出现这个错误呢?
【问题讨论】:
-
删除您创建的迁移文件,然后运行
rails db:migrate并再次检查您的 db/schema.rb(它应该重新生成一个新文件)。如果您缺少应该创建该列的迁移文件(您应该检查),那么您可能已经以其他方式创建了它; (手动,在测试时在另一个分支中并且您忘记回滚)因此考虑您可能需要撤消所做的操作(手动删除列,切换回其他分支并回滚)以便您可以根据需要创建迁移
标签: ruby-on-rails ruby postgresql activerecord