Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
f06fc6f5
Commit
f06fc6f5
authored
Dec 18, 2019
by
Steve Abrams
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add has_one section to foreign keys database docs
parent
0431f9d9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
0 deletions
+26
-0
doc/development/foreign_keys.md
doc/development/foreign_keys.md
+26
-0
No files found.
doc/development/foreign_keys.md
View file @
f06fc6f5
...
...
@@ -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
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment