-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding indexes on tables in migration scripts #16258
Conversation
@@ -109,6 +109,9 @@ class CreateOrders < ActiveRecord::Migration | |||
t.datetime :order_date | |||
t.timestamps | |||
end | |||
|
|||
add_index :orders, :customer_id | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this extra line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And in all your additions.
@mattwarrenrnp can you squash your commits together? |
travis failed on some intermittent error there unrelated to the patch.. |
@@ -109,6 +109,8 @@ class CreateOrders < ActiveRecord::Migration | |||
t.datetime :order_date | |||
t.timestamps | |||
end | |||
|
|||
add_index :orders, :customer_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use the :index
option of belongs_to
?
@mattwarrenrnp I noticed that most associations are defined with |
missed one migration script in last commit remove some empty lines using the belongs_to index option to be more concise
adding indexes on tables in migration scripts [ci skip]
@mattwarrenrnp thank you. |
I have seen several developers new (and old) to rails miss adding indexes to columns in migration scripts. In the vast majority of use cases adding indexes to foreign keys is best practice. As this guide is a top search result I feel that it's important to show best practices rather than a minimum working solution.
With so much magic in Rails, one may expect indexes to be added for you. Having them in the guide serves as a reminder that you need to explicitly add them yourself.