> ## Documentation Index
> Fetch the complete documentation index at: https://docs.upsolve.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Data Models

> Create curated views of your database with descriptions, annotations, and versioning.

## What is a Data Model?

A Data Model is a curated view of your database that:

* **Selects** which tables and columns are available
* **Describes** fields with human-readable annotations
* **Secures** data with row-level security rules
* **Versions** changes so you can track and rollback

<Frame>
  <img src="https://mintcdn.com/upsolve/fLb4mfHG-6nE-S3X/images/ai-agent-builder/data-model-overview.png?fit=max&auto=format&n=fLb4mfHG-6nE-S3X&q=85&s=7a0de9afccbdc2c941466bd93942f095" alt="Data model showing selected tables and columns" width="2920" height="1738" data-path="images/ai-agent-builder/data-model-overview.png" />
</Frame>

## Why Use Data Models?

1. **Simplify for AI** - Only expose relevant tables so AI agents aren't overwhelmed
2. **Add context** - Descriptions help AI understand what columns mean
3. **Control access** - Data Security rules ensure users see only their data
4. **Safe deployments** - Versioning prevents breaking changes

## Creating a Data Model

1. Navigate to your project's **Data Models** tab
2. Click **Create Data Model**
3. Select the connection to base it on
4. Enter a name for your data model
5. Click **Create**

The system will fetch all tables and columns from your connection automatically.

<Frame>
  <img src="https://mintcdn.com/upsolve/fLb4mfHG-6nE-S3X/images/ai-agent-builder/create-data-model.png?fit=max&auto=format&n=fLb4mfHG-6nE-S3X&q=85&s=21a249491d3855abb277ed44223e4be7" alt="Create data model dialog" width="1076" height="696" data-path="images/ai-agent-builder/create-data-model.png" />
</Frame>

## Configuring Your Data Model

### Selecting Tables and Columns

By default, all tables are selected. To customize:

1. Open your data model
2. Uncheck tables you want to hide
3. Expand tables to uncheck specific columns
4. Click **Save Changes**

<Frame>
  <img src="https://mintcdn.com/upsolve/fLb4mfHG-6nE-S3X/images/ai-agent-builder/select-columns.png?fit=max&auto=format&n=fLb4mfHG-6nE-S3X&q=85&s=02b372b94933b8a90f47156fbef4494f" alt="Table and column selection interface" width="2262" height="486" data-path="images/ai-agent-builder/select-columns.png" />
</Frame>

### Adding Descriptions

Help AI and users understand your data:

1. Click the edit icon next to a table or column
2. Enter a description
3. For AI agents, mark columns as "selectable" if they contain categorical values users might filter by

<Frame>
  <img src="https://mintcdn.com/upsolve/fLb4mfHG-6nE-S3X/images/ai-agent-builder/add-descriptions.png?fit=max&auto=format&n=fLb4mfHG-6nE-S3X&q=85&s=893b48bfb7b67a1f51d1d3b3c324b908" alt="Adding description to a column" width="2028" height="192" data-path="images/ai-agent-builder/add-descriptions.png" />
</Frame>

## Data Security

Data Security controls which rows of data each user can access. You can define **Global Security Rules** that apply across all tables, or configure per-table rules for specific needs.

### Understanding Data Security

The Data Security system uses SQL-based filtering with dynamic variables:

* **`{{user.*}}`** - Access properties of the currently authenticated user
* **`{{organization.*}}`** - Access properties of the user's organization

For example, to filter an `orders` table so users only see their own orders:

```sql theme={null}
SELECT *
FROM "orders"
WHERE "user_id" = '{{user.id}}'
```

### Global Security Rules

Global Security Rules let you define filtering logic once and apply it to all applicable tables automatically. This is ideal when you have consistent patterns like:

* Multi-tenant data separated by `tenant_id`
* User-owned data filtered by `user_id`
* Schema-based isolation (each tenant has their own database schema)

#### Accessing Global Rules

1. Open your data model
2. Click the **Data Security** tab in the left sidebar
3. You'll see the Global Rules editor

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/data-security-sidebar.png" alt="Data Security tab in sidebar showing global rules" />
</Frame>

#### Rule Types

**Schema Rules** - Dynamic schema prefixing for multi-tenant databases where each tenant has their own schema:

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/schema-rule-form.png" alt="Schema rule configuration form" />
</Frame>

This generates SQL like:

```sql theme={null}
SELECT *
FROM "{{user.schema}}"."orders"
```

**Column Rules** - Automatic WHERE clause filtering based on column names:

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/column-rule-form.png" alt="Column rule configuration form" />
</Frame>

This generates SQL like:

```sql theme={null}
SELECT *
FROM "orders"
WHERE "user_id" = '{{user.id}}'
```

#### Creating a Global Rule

1. Click the **Data Security** tab in the sidebar
2. Click **Add Rule**
3. Choose **Schema Rule** or **Column Rule**
4. Configure the rule settings
5. The rule is automatically enabled

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/add-rule-dialog.png" alt="Add rule dialog with rule type options" />
</Frame>

#### How Rules Are Applied

* **Schema rules** affect all tables - they change where data is fetched from
* **Column rules** only affect tables that have the specified column - tables without the column are unaffected
* Multiple rules are combined with AND logic
* Rules are applied in priority order (lower priority number = applied first)

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/rules-applied-preview.png" alt="Preview showing which tables are affected by rules" />
</Frame>

### Per-Table Data Security

When you select a table and go to its **Data Security** tab, you'll see how the global rules apply to that specific table.

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/table-data-security-global.png" alt="Per-table Data Security showing global rules are active" />
</Frame>

#### Manual Overrides (Break Glass)

Sometimes you need custom logic for a specific table. You can override global rules:

1. Select the table
2. Go to the **Data Security** tab
3. Click **Edit manually**
4. Confirm the warning dialog
5. Edit the SQL directly

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/edit-manually-dialog.png" alt="Warning dialog when switching to manual edit mode" />
</Frame>

Once in manual mode, you have full control over the SQL:

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/table-data-security-manual.png" alt="Per-table Data Security in manual override mode" />
</Frame>

#### Resetting to Global Rules

To return a table to global rule management:

1. Click **Reset to global**
2. Confirm the dialog
3. Your manual edits will be replaced with the auto-generated SQL

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/reset-to-global-dialog.png" alt="Confirmation dialog for resetting to global rules" />
</Frame>

### Testing Data Security

You can test your security rules to see exactly what data a specific user would see:

1. Select a table
2. Go to the **Data Security** tab
3. Select a user from the top-right user dropdown
4. Click **Test Query**
5. View the filtered results

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/upsolve/images/ai-agent-builder/test-rls-results.png" alt="RLS testing showing filtered results for a user" />
</Frame>

<Note>
  Testing uses your actual database connection and shows real data filtered by the security rules. This helps verify your configuration before deploying.
</Note>

## Versioning

Every change to a data model creates a new version. This means:

* You can see the history of changes
* You can compare versions
* Agents and applications link to specific versions

### Viewing Versions

1. Open your data model
2. Click the **Versions** tab in the sidebar
3. See all versions with timestamps and change information

<Frame>
  <img src="https://mintcdn.com/upsolve/uChIQDlSatTJLXIe/images/ai-agent-builder/versions-list.png?fit=max&auto=format&n=uChIQDlSatTJLXIe&q=85&s=ef861692519cdbc2c4181c60e2d9df06" alt="Data model versions list" width="1112" height="374" data-path="images/ai-agent-builder/versions-list.png" />
</Frame>

## Production Status

Before agents and applications can use a data model in production, you must mark a version as "Production."

### Setting Production

1. Open your data model
2. Go to the **Versions** tab
3. Click **Set as Production** on the version you want
4. The system validates the schema against your connection
5. If successful, the version is now production

<Frame>
  <img src="https://mintcdn.com/upsolve/fLb4mfHG-6nE-S3X/images/ai-agent-builder/set-production.png?fit=max&auto=format&n=fLb4mfHG-6nE-S3X&q=85&s=a239336dacf238e1295b9cac0aa8c714" alt="Set version as production button" width="884" height="402" data-path="images/ai-agent-builder/set-production.png" />
</Frame>

### Validation

When setting production, the system checks:

* All tables in your data model exist in the connection
* All columns in your data model exist in their tables
* If validation fails, you'll see which tables/columns are missing

<Note>
  If your database schema changes, you may need to update your data model to match.
</Note>

## Syncing with Connection Changes

If your database schema changes:

1. Open your data model
2. Go to the **Connection** tab
3. Click **Refresh Schema**
4. New tables/columns will appear (you can select them)
5. Missing tables/columns will show warnings

<Frame>
  <img src="https://mintcdn.com/upsolve/fLb4mfHG-6nE-S3X/images/ai-agent-builder/sync-connection.png?fit=max&auto=format&n=fLb4mfHG-6nE-S3X&q=85&s=badea60f21e0c699b1c8c8dd4b1e0ac6" alt="Sync with connection showing new and missing items" width="898" height="1122" data-path="images/ai-agent-builder/sync-connection.png" />
</Frame>

## Data Model Errors

If a production data model has errors (e.g., missing tables), you'll see a warning banner. This typically happens when:

* A table was dropped from the database
* A column was renamed or removed
* The connection credentials changed

## Next Steps

* [Create an Agent](/ai-agent-builder/agents) that uses this data model
* [Build an Application](/ai-agent-builder/applications) for dashboards
* [Manage Project Users](/ai-agent-builder/users) to define user properties for Data Security
