diff options
author | Zachary Scott <e@zzak.io> | 2015-04-12 20:20:48 -0700 |
---|---|---|
committer | Zachary Scott <e@zzak.io> | 2015-04-12 20:20:48 -0700 |
commit | f1af967e0055a33c45071848a049ff342e9c291e (patch) | |
tree | 126870d566701ae3834a8d7f78a26009f66d9774 /guides/source | |
parent | 93d2eeaad5f7e2a362297384fb840461980f675e (diff) | |
download | rails-f1af967e0055a33c45071848a049ff342e9c291e.tar.gz rails-f1af967e0055a33c45071848a049ff342e9c291e.tar.bz2 rails-f1af967e0055a33c45071848a049ff342e9c291e.zip |
Add note wrt foreign key constraint to ensure data integrity
Thanks for patch @sgrif :trollface:
Closes #18216
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/association_basics.md | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index 113cfcc274..498bc449d2 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -146,6 +146,16 @@ class CreateSuppliers < ActiveRecord::Migration end ``` +If you happen to want to create a unique index with foreign key constraint... +Then I would suggest using this for creating the accounts table instead: + +```ruby + create_table :accounts do |t| + t.belongs_to :supplier, index: true, unique: true, foreign_key: true + # ... + end +``` + ### The `has_many` Association A `has_many` association indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a `belongs_to` association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this: |