Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-27 15:07:59 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-27 15:07:59 +0300
commitaf8c2a780dc52175fcb9e79ad516772202b88aec (patch)
tree16ff41ca3972e90a46a8b89663c73f98e1059bd6 /doc/development/foreign_keys.md
parent59026a49b3affb3c6c3bc6ecc67b20c811623e70 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'doc/development/foreign_keys.md')
-rw-r--r--doc/development/foreign_keys.md26
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/development/foreign_keys.md b/doc/development/foreign_keys.md
index 0ab0deb156f..38b60ce6f0b 100644
--- a/doc/development/foreign_keys.md
+++ b/doc/development/foreign_keys.md
@@ -61,3 +61,29 @@ introduces non database logic to a model, and means we can no longer rely on
foreign keys to remove the data as this would result in the filesystem data
being left behind. In such a case you should use a service class instead that
takes care of removing non database data.
+
+## Alternative primary keys with has_one associations
+
+Sometimes a `has_one` association is used to create a one-to-one relationship:
+
+```ruby
+class User < ActiveRecord::Base
+ has_one :user_config
+end
+
+class UserConfig < ActiveRecord::Base
+ belongs_to :user
+end
+```
+
+In these cases, there may be an opportunity to remove the unnecessary `id`
+column on the associated table, `user_config.id` in this example. Instead,
+the originating table ID can be used as the primary key for the associated
+table:
+
+```ruby
+create_table :user_configs, id: false do |t|
+ t.references :users, primary_key: true, default: nil, index: false, foreign_key: { on_delete: :cascade }
+ ...
+end
+```