Skip to content

Commit 9016e22

Browse files
committed
Adds 'References' section to migrations guide, sorts column modifiers lists and adds 'collation' option documentation
* Creates 'References' section in ActiveRecord migrations guide and moves all 'add_reference' specific column modifiers information into this section * Adds the :collation option to the list of column modifiers section of the ActiveRecord migrations guide * Adds the :collation option to the documentation of ConnectionAdapters::SchemaStatements#add_column * Improves both lists of column modifiers by copying from one another * Sorts the lists of column modifiers alphabetically
1 parent 2ab30a5 commit 9016e22

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ def drop_table(table_name, **options)
518518

519519
# Add a new +type+ column named +column_name+ to +table_name+.
520520
#
521+
# See {ActiveRecord::ConnectionAdapters::TableDefinition.column}[rdoc-ref:ActiveRecord::ConnectionAdapters::TableDefinition#column].
522+
#
521523
# The +type+ parameter is normally one of the migrations native types,
522524
# which is one of the following:
523525
# <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>,
@@ -530,12 +532,17 @@ def drop_table(table_name, **options)
530532
# agnostic and should usually be avoided.
531533
#
532534
# Available options are (none of these exists by default):
535+
# * <tt>:comment</tt> -
536+
# Specifies the comment for the column. This option is ignored by some backends.
537+
# * <tt>:collation</tt> -
538+
# Specifies the collation for a <tt>:string</tt> or <tt>:text</tt> column.
539+
# If not specified, the column will have the same collation as the table.
540+
# * <tt>:default</tt> -
541+
# The column's default value. Use +nil+ for +NULL+.
533542
# * <tt>:limit</tt> -
534543
# Requests a maximum column length. This is the number of characters for a <tt>:string</tt> column
535544
# and number of bytes for <tt>:text</tt>, <tt>:binary</tt>, <tt>:blob</tt>, and <tt>:integer</tt> columns.
536545
# This option is ignored by some backends.
537-
# * <tt>:default</tt> -
538-
# The column's default value. Use +nil+ for +NULL+.
539546
# * <tt>:null</tt> -
540547
# Allows or disallows +NULL+ values in the column.
541548
# * <tt>:precision</tt> -

guides/source/active_record_migrations.md

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ class AddUserRefToProducts < ActiveRecord::Migration[6.0]
251251
end
252252
```
253253

254-
This migration will create a `user_id` column and appropriate index.
254+
This migration will create a `user_id` column, [references](#references) are a
255+
shorthand for creating columns, indexes, foreign keys or even polymorphic
256+
association columns.
255257

256258
There is also a generator which will produce join tables if `JoinTable` is part of the name:
257259

@@ -347,8 +349,7 @@ create_table :products do |t|
347349
end
348350
```
349351

350-
which creates a `products` table with a column called `name` (and as discussed
351-
below, an implicit `id` column).
352+
which creates a `products` table with a column called `name`.
352353

353354
By default, `create_table` will create a primary key called `id`. You can change
354355
the name of the primary key with the `:primary_key` option (don't forget to
@@ -364,6 +365,16 @@ end
364365

365366
will append `ENGINE=BLACKHOLE` to the SQL statement used to create the table.
366367

368+
An index can be created on the columns created within the `create_table` block
369+
by passing true or an options hash to the `:index` option:
370+
371+
```ruby
372+
create_table :users do |t|
373+
t.string :name, index: true
374+
t.string :email, index: { unique: true, name: 'unique_emails' }
375+
end
376+
```
377+
367378
Also you can pass the `:comment` option with any description for the table
368379
that will be stored in database itself and can be viewed with database administration
369380
tools, such as MySQL Workbench or PgAdmin III. It's highly recommended to specify
@@ -469,23 +480,65 @@ example, this would make your migration irreversible.
469480

470481
Column modifiers can be applied when creating or changing a column:
471482

472-
* `limit` Sets the maximum size of the `string/text/binary/integer` fields.
473-
* `precision` Defines the precision for the `decimal` fields, representing the
474-
total number of digits in the number.
475-
* `scale` Defines the scale for the `decimal` fields, representing the
476-
number of digits after the decimal point.
477-
* `polymorphic` Adds a `type` column for `belongs_to` associations.
478-
* `null` Allows or disallows `NULL` values in the column.
483+
* `comment` Adds a comment for the column.
484+
* `collation` Specifies the collation for a `string` or `text` column.
479485
* `default` Allows to set a default value on the column. Note that if you
480486
are using a dynamic value (such as a date), the default will only be calculated
481-
the first time (i.e. on the date the migration is applied).
482-
* `comment` Adds a comment for the column.
487+
the first time (i.e. on the date the migration is applied). Use `nil` for `NULL`.
488+
* `limit` Sets the maximum number of characters for a `string` column
489+
and the maximum number of bytes for `text/binary/integer` columns.
490+
* `null` Allows or disallows `NULL` values in the column.
491+
* `precision` Specifies the precision for `decimal/numeric/datetime/time` columns.
492+
* `scale` Specifies the scale for the `decimal` and `numeric` columns,
493+
representing the number of digits after the decimal point.
494+
495+
NOTE: For `add_column` or `change_column` there is no option for adding indexes.
496+
They need to be added separately using `add_index`.
483497

484498
Some adapters may support additional options; see the adapter specific API docs
485499
for further information.
486500

487501
NOTE: `null` and `default` cannot be specified via command line.
488502

503+
### References
504+
505+
The `add_reference` method allows the creation of an appropriately named column.
506+
507+
```ruby
508+
add_reference :users, :role
509+
```
510+
511+
This migration will create a `role_id` column in the users table. It creates an
512+
index for this column as well, unless explicitly told not with the
513+
`index: false` option:
514+
515+
```ruby
516+
add_reference :users, :role, index: false
517+
```
518+
519+
The method `add_belongs_to` is an alias of `add_reference`.
520+
521+
```ruby
522+
add_belongs_to :taggings, :taggable, polymorphic: true
523+
```
524+
525+
The polymorphic option will create two columns on the taggings table which can
526+
be used for polymorphic assocations: `taggable_type` and `taggable_id`.
527+
528+
A foreign key can be created with the the `foreign_key` option.
529+
530+
```ruby
531+
add_reference :users, :role, foreign_key: true
532+
```
533+
534+
For more `add_reference` options, visit the [API documentation](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_reference).
535+
536+
References can also be removed:
537+
538+
```ruby
539+
remove_reference :products, :user, foreign_key: true, index: false
540+
```
541+
489542
### Foreign Keys
490543

491544
While it's not required you might want to add foreign key constraints to

0 commit comments

Comments
 (0)