# Data Model: Tenant RBAC v1

This document outlines the data model for the Tenant RBAC feature, as defined in the feature specification.

## Tables

### `tenant_memberships` (New Table)

This table is the source of truth for user membership and roles within a tenant.

**Columns**:

| Name | Type | Description | Constraints |
|---|---|---|---|
| `id` | `bigint` or `uuid` | Primary key. Follows repository convention. | Primary Key |
| `tenant_id` | `bigint` | Foreign key to the `tenants` table. | Not Null, FK to `tenants.id` |
| `user_id` | `bigint` | Foreign key to the `users` table. | Not Null, FK to `users.id` |
| `role` | `string` | The user's role within the tenant. | Not Null, Enum: `owner`, `manager`, `operator`, `readonly` |
| `created_at` | `timestamp` | Timestamp of creation. | Not Null |
| `updated_at` | `timestamp` | Timestamp of last update. | Not Null |

**Indexes**:

-   `tenant_memberships_tenant_id_user_id_unique`: Unique constraint on `(tenant_id, user_id)` to ensure a user has only one role per tenant.
-   `tenant_memberships_tenant_id_role_index`: Index on `(tenant_id, role)` for efficient role-based queries within a tenant.
-   `tenant_memberships_user_id_index`: Index on `(user_id)` for efficiently finding all tenant memberships for a user.

## Relationships

-   A `Tenant` has many `TenantMembership` records.
-   A `User` has many `TenantMembership` records.
-   A `TenantMembership` belongs to one `Tenant` and one `User`.
