create
create
A create action on a store resource. Defines how new records are inserted, which fields are accepted, what validations run before insert, and what changes apply during the operation. Compiles to an Ash Framework create action at runtime.
When to use
Use create when you need to:
- Insert new records into a store resource
- Restrict which fields can be set during creation
- Validate input data before persisting (e.g., required fields, format checks)
- Apply computed changes during insertion (e.g., set a default status, generate a slug)
Every resource should have at least one create action. Use defaults: [create] for a standard create action that accepts all fields, or define a named action with explicit accept and validate blocks for fine-grained control.
Syntax
resource <resource_name> create <action_name> accept: [<field1>, <field2>, ...] validate <rule> change <function>Parameters
| Parameter | Required | Description |
|---|---|---|
action_name | Yes | Bare identifier naming this action. Multiple create actions can exist on a single resource. |
accept | No | List of field names that this action accepts. If omitted, all non-primary-key fields are accepted. |
validate | No | Validation rule applied before insert. Can appear multiple times. |
change | No | Mutation function applied during insert. Can appear multiple times. |
Validation rules
| Rule | Description | Example |
|---|---|---|
present([fields]) | Fields must be non-nil | validate present([title, body]) |
format(field, pattern) | Field must match a regex pattern | validate format(email, ~r/@/) |
length(field, min, max) | String length within range | validate length(name, 1, 100) |
compare(field, op, value) | Numeric comparison | validate compare(age, :gt, 0) |
Examples
Simple create with accepted fields
resource document id as uuid, is primary_key title as text, is required content as text status as text, default: "draft" timestamps
create add_document accept: [title, content]The status field is not in the accept list, so it always gets its default value of "draft" when a new record is created through this action.
Create with validation
resource user id as uuid, is primary_key email as text, is required username as text, is required bio as text timestamps
create register accept: [email, username, bio] validate present([email, username]) validate format(email, ~r/@/) validate length(username, 3, 30)Multiple create actions on one resource
resource ticket id as uuid, is primary_key title as text, is required priority as text, default: "normal" source as text, is required timestamps
create from_web accept: [title, priority] change set_attribute(source, "web")
create from_api accept: [title, priority] change set_attribute(source, "api")
create from_email accept: [title] change set_attribute(source, "email") change set_attribute(priority, "normal")Governance
Create actions are governed effects. When a machine executes a create action:
- The governance interpreter checks the
dbcapability - The action is subject to any
policyblocks on the resource - The operation is recorded in the behavioral ledger with the action name, accepted fields, and result
- The machine’s
ensures > permissionssection can restrict or require approval for data writes
Translations
| Language | Keyword |
|---|---|
| English | create |
| Spanish | crear |
| French | creer |
| German | erstellen |
| Japanese | 作成 |
| Chinese | 创建 |
| Korean | 생성 |