From 791039c131fffe4a356d61f4ed9166917551e1a8 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Mon, 4 Dec 2023 18:04:52 +0200 Subject: [PATCH] missing_unique_indexes: handle foreign keys on primary keys This commit fixes another issue stemming from foreign keys defined on primary keys. This time the issue affected missing_unique_indexes detector. --- .../detectors/missing_unique_indexes.rb | 1 + .../detectors/missing_unique_indexes_test.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/active_record_doctor/detectors/missing_unique_indexes.rb b/lib/active_record_doctor/detectors/missing_unique_indexes.rb index 134c95f..43ab283 100644 --- a/lib/active_record_doctor/detectors/missing_unique_indexes.rb +++ b/lib/active_record_doctor/detectors/missing_unique_indexes.rb @@ -97,6 +97,7 @@ def has_ones_without_indexes # rubocop:disable Naming/PredicateName table_name = has_one.klass.table_name next if unique_index?(table_name, columns) + next if Array(connection.primary_key(table_name)) == columns problem!(model: model, table: table_name, columns: columns, problem: :has_ones) end diff --git a/test/active_record_doctor/detectors/missing_unique_indexes_test.rb b/test/active_record_doctor/detectors/missing_unique_indexes_test.rb index d56805e..fa3f859 100644 --- a/test/active_record_doctor/detectors/missing_unique_indexes_test.rb +++ b/test/active_record_doctor/detectors/missing_unique_indexes_test.rb @@ -426,6 +426,22 @@ def test_polymorphic_has_one_with_index refute_problems end + def test_has_one_on_primary_key_column + Context + .create_table(:parents) + .define_model do + has_one :child + end + + Context + .create_table(:children, primary_key: :parent_id) + .define_model do + belongs_to :parent + end + + refute_problems + end + def test_config_ignore_models Context.create_table(:users) do |t| t.string :email