# Multi-tenant

Reuse your existing multi-tenant definitions and RBAC. The most common setup is one tenant (typically organization_id or user_id), but we support parent-child relationships, and any relationship you can write in SQL.

* **Tenant field**: the foreign key that separates data by **Tenant ID** on a table. We use this field to validate that a table in the Quill semantic layer can be referenced in dashboards owned by that tenant.
* **Tenant ID**: the id value that corresponds to one specific tenant value (ex: organization_id=1)

## Row vs database tenancy

Row tenancy keeps every tenant in the same tables, told apart by a column.
Database tenancy gives each tenant a database of its own.

```mermaid tenancy
flowchart TB
  subgraph row["Row tenancy"]
    orders[("orders")]
    orders --- acme1["organization_id = acme"]
    orders --- northwind1["organization_id = northwind"]
  end
  subgraph db["Database tenancy"]
    acme2[("acme_db")]
    northwind2[("northwind_db")]
  end
```

## Tenant ownership

A semantic layer table is owned by at least one tenant type (except for specialized tables that don't house customer data like constant tables). Ownership is verified by tenant field: the semantic layer table must have the tenant field of a tenant type that owns it.

```mermaid tenant-ownership
flowchart LR
  subgraph org["Owned by Organization"]
    sales["Sales dashboard"] --> orgorders[("orders")]
    sales --> invoices[("invoices")]
  end
  subgraph user["Owned by User"]
    myorders["My orders dashboard"] --> userorders[("orders")]
  end
  myorders -.-x invoices
```

Each dashboard is owned by one tenant type. It can only use virtual tables and saved queries that are owned by that tenant type.

| Dashboard (owner tenant) | orders (Organization, User) | invoices (Organization) |
| ------------------------ | :-------------------------: | :---------------------: |
| Sales (Organization)     |              ✓              |            ✓            |
| My orders (User)         |              ✓              |            —            |

## Tenant relationships

A mapping is a SQL query that defines the relationship between tenants. The most common example is parent-child, but you can define sibling relationship (ex: the client, vendor, and accountant roles in invoicing software)

```mermaid tenant-relationships
flowchart LR
  acme["Parent Company"] --> ada["Supply Company"]
  acme --> alan["Transportation Company"]
```

## Report tenant access

A report is visible to every tenant by default (Global). Admins can only show a report to a subset of end users by id and/or tenant type.

```mermaid report-access
flowchart LR
  report["Custom report"] --> acme["Acme"]
  report --> initech["Initech"]
  report -.-x northwind["Northwind"]
```
