Skip to content

Commit 977e405

Browse files
authored
Update README for ignore_list as well as missing fk indexes (#35)
1 parent a74821a commit 977e405

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Keep reading to learn about methods that `diagnose` uses under the hood.
118118

119119
### `missing_fk_indexes`
120120

121-
This method lists columns likely to be foreign keys (i.e. column name ending in `_id` and related table exists) which don't have an index. It's recommended to always index foreign key columns because they are used for searching relation objects.
121+
This method lists **actual foreign key columns** (based on existing foreign key constraints) which don't have a supporting index. It's recommended to always index foreign key columns because they are commonly used for lookups and join conditions.
122122

123123
You can add indexes on the columns returned by this query and later check if they are receiving scans using the [unused_indexes method](#unused_indexes). Please remember that each index decreases write performance and autovacuuming overhead, so be careful when adding multiple indexes to often updated tables.
124124

@@ -140,10 +140,20 @@ RubyPgExtras.missing_fk_indexes(args: { table_name: "users" })
140140

141141
## `missing_fk_constraints`
142142

143-
Similarly to the previous method, this one shows columns likely to be foreign keys that don't have a corresponding foreign key constraint. Foreign key constraints improve data integrity in the database by preventing relations with nonexisting objects. You can read more about the benefits of using foreign keys [in this blog post](https://pawelurbanek.com/rails-postgresql-data-integrity).
143+
This method shows **columns that look like foreign keys** but don't have a corresponding foreign key constraint yet. Foreign key constraints improve data integrity in the database by preventing relations with nonexisting objects. You can read more about the benefits of using foreign keys [in this blog post](https://pawelurbanek.com/rails-postgresql-data-integrity).
144+
145+
Heuristic notes:
146+
- A column is considered a candidate if it matches `<table_singular>_id` and the related table exists (underscored prefixes like `account_user_id` are supported).
147+
- Rails polymorphic associations (`<name>_id` + `<name>_type`) are ignored since they cannot be expressed as real FK constraints.
148+
149+
You can also exclude known/intentional cases using `ignore_list` (array or comma-separated string), with entries like:
150+
- `"posts.category_id"` (ignore a specific table+column)
151+
- `"category_id"` (ignore this column name for all tables)
152+
- `"posts.*"` (ignore all columns on a table)
153+
- `"*"` (ignore everything)
144154

145155
```ruby
146-
RubyPgExtras.missing_fk_constraints(args: { table_name: "users" })
156+
RubyPgExtras.missing_fk_constraints(args: { table_name: "users", ignore_list: ["users.customer_id", "posts.*"] })
147157

148158
+---------------------------------+
149159
| Missing foreign key constraints |

0 commit comments

Comments
 (0)