Skip to content

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

ParameterRequiredDescription
action_nameYesBare identifier naming this action. Multiple create actions can exist on a single resource.
acceptNoList of field names that this action accepts. If omitted, all non-primary-key fields are accepted.
validateNoValidation rule applied before insert. Can appear multiple times.
changeNoMutation function applied during insert. Can appear multiple times.

Validation rules

RuleDescriptionExample
present([fields])Fields must be non-nilvalidate present([title, body])
format(field, pattern)Field must match a regex patternvalidate format(email, ~r/@/)
length(field, min, max)String length within rangevalidate length(name, 1, 100)
compare(field, op, value)Numeric comparisonvalidate 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:

  1. The governance interpreter checks the db capability
  2. The action is subject to any policy blocks on the resource
  3. The operation is recorded in the behavioral ledger with the action name, accepted fields, and result
  4. The machine’s ensures > permissions section can restrict or require approval for data writes

Translations

LanguageKeyword
Englishcreate
Spanishcrear
Frenchcreer
Germanerstellen
Japanese作成
Chinese创建
Korean생성

See also

  • resource - Resource declarations
  • read - Read actions for querying data
  • update - Update actions for modifying records
  • destroy - Destroy actions for deleting records
  • policy - Authorization policies on actions