我有兩種模式。一個Events
模型和一個EventOption
。 Events
將have_many
:event_options
。Rails5 - 在現有模型中添加外鍵到postgres
我的問題是,當我嘗試做遷移到add_foreign key :event_options, :events
,這樣我可以聯繫他們,我得到以下錯誤:
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "event_id" referenced in foreign key constraint does not exist
: ALTER TABLE "event_options" ADD CONSTRAINT "fk_rails_3995702fad"
FOREIGN KEY ("event_id")
REFERENCES "events" ("id")
這裏是我的schema.rb:
ActiveRecord::Schema.define(version: 20160806001743) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "event_options", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "price"
t.text "description"
t.string "name"
end
create_table "events", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.boolean "active", default: true
end
create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", null: false
t.string "encrypted_password", limit: 128, null: false
t.string "confirmation_token", limit: 128
t.string "remember_token", limit: 128, null: false
t.index ["email"], name: "index_users_on_email", using: :btree
t.index ["remember_token"], name: "index_users_on_remember_token", using: :btree
end
end
我知道有:id列工作,因爲我可以在控制檯中與他們一起玩。我知道我在這裏錯過了一些東西來讓外鍵爲應用程序工作,但在我的生活中,我不知道是什麼。
它想'event_id'你需要在你的'event_options'表,否則'has_many'不會工作,要麼 –