Skip to content
Advertisement

rails – shouldn’t I use default values in rails migration

I am not sure whether I should ask this question on SO or not. But I have to as I don’t have clear idea about my doubt. I was told not to set default values in rails migrations and use null values instead in a code review by our CTO. And his explanation is like this once the database gets huge enough, migrations with defaults runs slow and may throw database timeout errors. But I think at the database level it is fast enough to execute as its an initial setup of things. Am I right?

Advertisement

Answer

Using default values in add column migrations takes time when number of table rows is huge.

This is a tip given by Postgres Doc:https://www.postgresql.org/docs/9.5/static/ddl-alter.html

Tip: Adding a column with a default requires updating each row of the table (to store the new column value). However, if no default is specified, PostgreSQL is able to avoid the physical update. So if you intend to fill the column with mostly nondefault values, it’s best to add the column with no default, insert the correct values using UPDATE, and then add any desired default as described below.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement