@@ -251,7 +251,9 @@ class AddUserRefToProducts < ActiveRecord::Migration[7.0]
251
251
end
252
252
```
253
253
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.
255
257
256
258
There is also a generator which will produce join tables if ` JoinTable ` is part of the name:
257
259
@@ -347,8 +349,7 @@ create_table :products do |t|
347
349
end
348
350
```
349
351
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 ` .
352
353
353
354
By default, ` create_table ` will create a primary key called ` id ` . You can change
354
355
the name of the primary key with the ` :primary_key ` option (don't forget to
364
365
365
366
will append ` ENGINE=BLACKHOLE ` to the SQL statement used to create the table.
366
367
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
+
367
378
Also you can pass the ` :comment ` option with any description for the table
368
379
that will be stored in database itself and can be viewed with database administration
369
380
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.
469
480
470
481
Column modifiers can be applied when creating or changing a column:
471
482
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.
479
485
* ` default ` Allows to set a default value on the column. Note that if you
480
486
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 ` .
483
497
484
498
Some adapters may support additional options; see the adapter specific API docs
485
499
for further information.
486
500
487
501
NOTE: ` null ` and ` default ` cannot be specified via command line.
488
502
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
+
489
542
### Foreign Keys
490
543
491
544
While it's not required you might want to add foreign key constraints to
0 commit comments