0
我正在使用Rails 5和PostGres 9.5。我想创建从我search_codes表的外键到我的地址表,所以我创造了这个迁移如何使用Rails 5迁移中的级联删除约束创建外键?
class CreateSearchCodeTable < ActiveRecord::Migration[5.0]
def change
create_table :search_codes do |t|
t.string :code
t.references :address, type: :string, index: true, foreign_key: true, on_delete: :cascade
t.index ["code"], name: "index_search_codes_on_code", unique: true, using: :btree
end
end
end
但是什么被创建如下。通知似乎约束的级联部分没有得到创建
myapp=> \d search_codes;
Table "public.search_codes"
Column | Type | Modifiers
------------+-------------------+-----------------------------------------------------------
id | integer | not null default nextval('search_codes_id_seq'::regclass)
code | character varying |
address_id | character varying |
Indexes:
"search_codes_pkey" PRIMARY KEY, btree (id)
"index_search_codes_on_code" UNIQUE, btree (code)
"index_search_codes_on_address_id" btree (address_id)
Foreign-key constraints:
"fk_rails_6bd9792e3b" FOREIGN KEY (address_id) REFERENCES addresses(id)
如何创建使用Rails的5迁移和Postgres级联的外键约束迁移?
很酷。我讨厌它,当应该工作的东西(如指定“on_delete”)不。 – Dave