add_reference, ... index: true should be banded

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

  • Close this issue

Problem

When you create a new column, add a new foreign key, and add an index, add_reference method is convenient. For exmaple, you can code like

  def change
    add_reference :model, :other_model, index: true, foreign_key: { on_delete: :cascade }
  end

Then, it handles the above operations automagically. This is also described in our migration style guide

However, add_reference does not use { algorithm: :concurrently } by default. If accidentally someone ships the above migration to production, downtime would occur, as it locks table to add an index.

Solution

  • Ban add_reference, ... index: true usage
  • Alternatively, we can introduce add_concurrent_reference method
  • Alternatively, we can recommend manual way. (use add_column, add_concurrent_index, add_concurrent_foreign_key example)
  • Update migration style guide
Edited Sep 13, 2025 by 🤖 GitLab Bot 🤖
Assignee Loading
Time tracking Loading