Skip to content

Commit d81d41d

Browse files
Update documentation for create_join_table
Follow up of rails#28217. `column_options` must not be sent inside `options` anymore. Also, add an example about how to use delete cascade.
1 parent 730367e commit d81d41d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,13 @@ def build_create_table_definition(table_name, id: :primary_key, primary_key: nil
354354
# # Creates a table called 'music_artists_records' with no id.
355355
# create_join_table('music_artists', 'music_records')
356356
#
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+
#
357361
# You can pass an +options+ hash which can include the following keys:
358362
# [<tt>:table_name</tt>]
359363
# Sets the table name, overriding the default.
360-
# [<tt>:column_options</tt>]
361-
# Any extra options you want appended to the columns definition.
362364
# [<tt>:options</tt>]
363365
# Any extra options you want appended to the table definition.
364366
# [<tt>:temporary</tt>]
@@ -375,6 +377,19 @@ def build_create_table_definition(table_name, id: :primary_key, primary_key: nil
375377
# t.index :category_id
376378
# end
377379
#
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+
#
378393
# ====== Add a backend specific option to the generated SQL (MySQL)
379394
#
380395
# create_join_table(:assemblies, :parts, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8')

0 commit comments

Comments
 (0)