@@ -354,11 +354,13 @@ def build_create_table_definition(table_name, id: :primary_key, primary_key: nil
354
354
# # Creates a table called 'music_artists_records' with no id.
355
355
# create_join_table('music_artists', 'music_records')
356
356
#
357
+ # See {connection.add_reference}[rdoc-ref:SchemaStatements#add_reference]
358
+ # for details of the options you can use in +column_options+. +column_options+
359
+ # will be applied to both columns.
360
+ #
357
361
# You can pass an +options+ hash which can include the following keys:
358
362
# [<tt>:table_name</tt>]
359
363
# Sets the table name, overriding the default.
360
- # [<tt>:column_options</tt>]
361
- # Any extra options you want appended to the columns definition.
362
364
# [<tt>:options</tt>]
363
365
# Any extra options you want appended to the table definition.
364
366
# [<tt>:temporary</tt>]
@@ -375,6 +377,19 @@ def build_create_table_definition(table_name, id: :primary_key, primary_key: nil
375
377
# t.index :category_id
376
378
# end
377
379
#
380
+ # ====== Add foreign keys with delete cascade
381
+ #
382
+ # create_join_table(:assemblies, :parts, column_options: { foreign_key: { on_delete: :cascade } })
383
+ #
384
+ # generates:
385
+ #
386
+ # CREATE TABLE assemblies_parts (
387
+ # assembly_id bigint NOT NULL,
388
+ # part_id bigint NOT NULL,
389
+ # CONSTRAINT fk_rails_0d8a572d89 FOREIGN KEY ("assembly_id") REFERENCES "assemblies" ("id") ON DELETE CASCADE,
390
+ # CONSTRAINT fk_rails_ec7b48402b FOREIGN KEY ("part_id") REFERENCES "parts" ("id") ON DELETE CASCADE
391
+ # )
392
+ #
378
393
# ====== Add a backend specific option to the generated SQL (MySQL)
379
394
#
380
395
# create_join_table(:assemblies, :parts, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8')
0 commit comments