# `Alternera.Resource.Transformers.AddTenantReferences`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_resource_sdk/lib/alternera/resource/transformers/add_tenant_references.ex#L1)

Keeps every reference between records inside one tenant, in the database.

Each `belongs_to` of the resource gets an AshPostgres reference with
`match_with: [tenant_id: :tenant_id]` and `match_type: :simple`, so its
foreign key is `(<ref>_id, tenant_id)` → `(id, tenant_id)` and the database
refuses a record that names another tenant's; `SIMPLE` lets an empty
reference pass. Not AshPostgres's `match_tenant?`: it adds the tenant
column only when the migration is rendered, so the generator does not
order the key after the target's unique index and Postgres refuses it.
A reference the package declared keeps its other options (`on_delete`, a
name); one it did not declare is added with `on_delete: :restrict`.

The foreign key needs a unique key on the target's `(tenant_id, <primary
key>)`, so every resource gets an identity `:alternera_tenant_key` on its
primary key, which attribute multitenancy makes a unique index on
`(tenant_id, <primary key>)`. It also serves every query by tenant.

# `after?`

# `after_compile?`

# `before?`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
