Raw SQL
Raw SQL fields execute custom PostgreSQL queries to produce computed values directly from the database.
Go to Studio → Collections → [Collection Name] → Fields tab → Add Field, then select Raw SQL. To edit an existing field, click the edit action next to it.
View all settings
| Setting | Description |
|---|---|
| Field Name | Text input, required. Unique identifier for the field. |
| Input Type | Dropdown, required. Choose Single, Array, Object, or Object Array. |
| Display Label | Text input. Human-readable label shown in forms. |
| Icon | Icon picker. Visual identifier. |
| Raw SQL | Code editor, required. PostgreSQL query to execute. |
| Status | Toggle, defaults to true. Enable or disable the field. |
| Required | Checkbox. Make the field mandatory. |
| Description | Text area. Documents the field's purpose. |
Input Types
- Single — Returns a single scalar value
- Array — Returns an array of values
- Object — Returns a JSON object (single row)
- Object Array — Returns an array of JSON objects (multiple rows)
When to Use
Choose Raw SQL when:
- You need database-specific PostgreSQL functions
- Complex calculations not possible with Compute fields
- Window functions or advanced aggregations
- Subqueries and cross-collection joins
Settings
See Common Settings for shared options.
Not available: Default Value, Primary Key, Unique, Faker Type, Form Visibility, DB Field Name
Raw SQL Editor (Basic Settings)
Code editor, required. Write PostgreSQL queries to compute the field value. The editor provides syntax highlighting for SQL.
SELECT * FROM users;
Advanced Settings
Only Description is available in Advanced Settings for this field type.
Example Expressions
-- Concatenate with formatting
CONCAT(first_name, ' ', last_name)
-- Date calculations
NOW() - created_at
-- Conditional logic
CASE WHEN status = 'active' THEN 1 ELSE 0 END
note
Raw SQL fields are read-only — no database column is created.
warning
Invalid SQL causes query errors. Test queries in a SQL editor before saving.
Related
- Common Settings — Shared settings reference
- Compute — For simpler calculations