Skip to content

Add partial index conditions for references#795

Merged
zachdaniel merged 2 commits into
ash-project:mainfrom
jechol:reference-index-where
Jul 16, 2026
Merged

Add partial index conditions for references#795
zachdaniel merged 2 commits into
ash-project:mainfrom
jechol:reference-index-where

Conversation

@jechol

@jechol jechol commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Adds index_where to references, supporting custom conditions and :not_nil.

Changing the condition recreates only the index without modifying the foreign key.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

@zachdaniel
zachdaniel merged commit 1f54832 into ash-project:main Jul 16, 2026
103 of 121 checks passed
jechol added a commit to devall-org/ash_postgres_belongs_to_index that referenced this pull request Jul 19, 2026
## Summary

- Nullable `belongs_to` relationships now get **partial indexes**
excluding `NULL` rows: references use `index_where: :not_nil`, custom
indexes use `where: "fk_id IS NOT NULL"`. FK lookups are equality
matches (always `IS NOT NULL`), so the smaller index serves them fully.
`allow_nil? false` relationships keep full indexes.
- On multitenant resources, the tenant attribute gets its own
single-column index when every other FK index is partial, since partial
FK indexes cannot serve tenant-only lookups.
- Combined with #2 on the multitenant single-column index:
`all_tenants?: true` + `include_base_filter?: false` + stable
`{table}_{fk_id}_fkey_index` name, plus the partial `where` when
nullable.
- Bumps version to 0.5.0 with changelog and README updates.

Requires ash_postgres with `index_where` on references
(ash-project/ash_postgres#795) — not yet in a hex release, dependency
stays on GitHub main.

## Test plan

- `mix test` (22 tests, including new `test/partial_index_test.exs`)
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