Skip to content

Commit

Permalink
missing_unique_indexes: handle foreign keys on primary keys
Browse files Browse the repository at this point in the history
This commit fixes another issue stemming from foreign keys defined
on primary keys. This time the issue affected missing_unique_indexes
detector.
  • Loading branch information
fatkodima committed Dec 4, 2023
1 parent a7e33c6 commit 791039c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/active_record_doctor/detectors/missing_unique_indexes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 791039c

Please sign in to comment.