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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
3d2a3e57
Commit
3d2a3e57
authored
Aug 01, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs: FK constraints require an index.
Closes #49789.
parent
be1ef711
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
0 deletions
+28
-0
doc/development/migration_style_guide.md
doc/development/migration_style_guide.md
+28
-0
No files found.
doc/development/migration_style_guide.md
View file @
3d2a3e57
...
...
@@ -182,6 +182,34 @@ class MyMigration < ActiveRecord::Migration
end
```
## Adding foreign-key constraints
When adding a foreign-key constraint to either an existing or new
column remember to also add a index on the column.
This is _required_ if the foreign-key constraint specifies
`ON DELETE CASCADE`
or
`ON DELETE SET NULL`
behavior. On a cascading
delete, the
[
corresponding record needs to be retrieved using an
index
](
https://www.cybertec-postgresql.com/en/postgresql-indexes-and-foreign-keys/
)
(otherwise, we'd need to scan the whole table) for subsequent update or
deletion.
Here's an example where we add a new column with a foreign key
constraint. Note it includes
`index: true`
to create an index for it.
```
ruby
class
Migration
<
ActiveRecord
::
Migration
def
change
add_reference
:model
,
:other_model
,
index:
true
,
foreign_key:
{
on_delete: :cascade
}
end
end
```
When adding a foreign-key constraint to an existing column, we
have to employ
`add_concurrent_foreign_key`
and
`add_concurrent_index`
instead of
`add_reference`
.
## Adding Columns With Default Values
When adding columns with default values you must use the method
...
...
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