Feat/match tenant reference - #813
Merged
zachdaniel merged 2 commits intoAug 12, 2026
Merged
Conversation
Allows opting into composite tenant matching on primary-key references so cross-tenant relationships can be blocked at the database (ash-project#192). Co-authored-by: Cursor <cursoragent@cursor.com>
Keep only the match_tenant? DSL docs row and add the spark formatter local so CI spark.formatter --check passes. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributor checklist
Leave anything that you believe does not apply unchecked.
Summary
match_tenant?onpostgres.references.reference(defaultfalse) so attribute-multitenant foreign keys can require matching tenants.with: [tenant: tenant], match: :fullon primary-key references, which blocks cross-tenant relationships at the database.Why
Ash used to always add the tenant column to composite FKs. That broke Postgres when the destination was a primary key (#144), because
(id, tenant)needs a unique index covering those columns. The earlier fix skipped the tenant column for PK references, which unblocked migrations but allowed cross-tenant rows to be inserted.Changes
lib/reference.ex— newmatch_tenant?optionlib/migration_generator/operation.ex— include tenant matching on PK refs when the option is onlib/migration_generator/migration_generator.ex— pass the option through snapshots.formatter.exs— registermatch_tenant?for Spark formatterdocumentation/dsls/DSL-AshPostgres.DataLayer.md— document the optiontest/migration_generator_test.exs— test thatmatch_tenant?: truegenerateswith: [org_id: :org_id], match: :fullUsage
Notes
Enabling this on a PK reference may still require a unique index covering
(tenant, id)on the destination (same Postgres constraint as #144). Happy to follow up if maintainers want that generated automatically.