title: How do I create a foreign key with a cascading delete constraint in my Rails 5 migration?
body: <p>I'm using Rails 5 with PostGres 9.5 . I want to create a foreign key to my addresses table from my search_codes table so I created this migration</p>
  
  <pre><code>class CreateSearchCodeTable &lt; 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
  </code></pre>
  
  <p>However what gets created is the following.  Notice the cascading portion of the constraint didn't seem to get created</p>
  
  <pre><code>myapp=&gt; \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)
  </code></pre>
  
  <p>How do I create a migration with a cascading foreign key constraint using the Rails 5 migration and PostGres?</p>