Skip to content

Commit

Permalink
unindexed_foreign_keys: handle foreign keys on primary keys
Browse files Browse the repository at this point in the history
This commit fixes an edge case where a foreign key is defined on the
primary key. Primary keys are already indexed and have to be unique,
so it's not necessary to report them.
  • Loading branch information
fatkodima authored Dec 4, 2023
1 parent b013fc0 commit a7e33c6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def detect
next unless named_like_foreign_key?(column) || foreign_key?(table, column)
next if indexed?(table, column)
next if indexed_as_polymorphic?(table, column)
next if connection.primary_key(table) == column.name

type_column_name = type_column_name(column)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def test_indexed_foreign_key_is_not_reported
refute_problems
end

def test_foreign_key_is_a_primary_key_not_reported
Context.create_table(:parents)
Context.create_table(:children, primary_key: :parent_id)
ActiveRecord::Base.connection.add_foreign_key(:children, :parents)

refute_problems
end

def test_config_ignore_tables
skip("MySQL always indexes foreign keys") if mysql?

Expand Down

0 comments on commit a7e33c6

Please sign in to comment.