Skip to content

unindexed_foreign_keys validation for composite primary key on HABTM#212

Closed
benaitcheson wants to merge 2 commits into
gregnavis:masterfrom
benaitcheson:fix/habtm-composite-key-index-fk-check
Closed

unindexed_foreign_keys validation for composite primary key on HABTM#212
benaitcheson wants to merge 2 commits into
gregnavis:masterfrom
benaitcheson:fix/habtm-composite-key-index-fk-check

Conversation

@benaitcheson

@benaitcheson benaitcheson commented Mar 4, 2025

Copy link
Copy Markdown

Fix false positive for HABTM tables with composite primary keys

Problem

The UnindexedForeignKeys detector currently reports false positives for HABTM
(Has And Belongs To Many) tables that use composite primary keys. It suggests
adding indexes that already exist as part of the primary key.

Solution

Modified the indexed? method to recognize columns that are part of composite
primary keys. This prevents false positives while maintaining the original
functionality for single-column indexes.

Example

Given a HABTM table:

CREATE TABLE companies_users (
  company_id bigint NOT NULL,
  user_id bigint NOT NULL,
  PRIMARY KEY (company_id, user_id)
);

Testing

Pointed the repo at this branch and ran the UnindexedForeignKeys command. Has fixed the issue for my repo.

@benaitcheson benaitcheson changed the title unindexed_foreign_keys validation for primary key on HABTM unindexed_foreign_keys validation for composite primary key on HABTM Mar 4, 2025
@fatkodima

Copy link
Copy Markdown
Contributor

I think this will be fixed as part of #148.

@benaitcheson

Copy link
Copy Markdown
Author

I think this will be fixed as part of #148.

Amazing! Thanks @fatkodima. Love your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants