Skip to content

ActiveRecord migrations with polymorphic references create indexes with different name #41693

Closed
@ClearlyClaire

Description

@ClearlyClaire

Steps to reproduce

Create a table with a polymorphic references in a < 6.0 migration, run the migration with ActiveRecord 6.

This was most probably introduced by e8437a6.

Minimal testcase
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "6.1.3"
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

class CreateSettings < ActiveRecord::Migration[5.0]
  def self.up
    create_table :settings do |t|
      t.references :target, null: false, polymorphic: true
    end
  end
end

class BugTest < Minitest::Test
  def test_migration_up
    CreateSettings.migrate(:up)
    assert CreateSettings.index_name_exists?(:settings, :index_settings_on_target_type_and_target_id)
  end
end

Expected behavior

The created index should have the same name when the migration runs in ActiveRecord 6 than it had in ActiveRecord 5, in this case, index_settings_on_target_type_and_target_id.

Actual behavior

The migration creates an index with a different name: index_settings_on_target.

System configuration

Rails version: 6.1.3

Ruby version: 2.7.2p137

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions