api/src/ai/tools/relations/prompt.md
Create and manage relationships between Directus Collections.
<prerequisites> Before creating relations: ✓ Collections must exist (use `collections` tool) ✓ Fields must be created with correct types (use `fields` tool) ✓ Junction collections must exist for M2M/M2A relationships ✓ Optional system user fields (`user_created`/`user_updated`) require relations to `directus_users` (see `collections` tool `` section) </prerequisites> <actions> - `create`: Establish relationship between collections - `read`: View existing relationships - `update`: Modify relationship settings (only schema.on_delete/on_update and meta can be updated) - `delete`: Remove relationships </actions><basic_relation> After creating relational fields, define the relationship:
{
"action": "create",
"data": {
"collection": "articles",
"field": "author",
"related_collection": "directus_users",
"meta": { "sort_field": null },
"schema": { "on_delete": "SET NULL" }
}
}
</basic_relation>
<relationship_types> <m2o_workflow>
Multiple items in one collection relate to one item in another.
Example: Many articles → one author
Complete M2O Workflow:
Add M2O field to the "many" collection → Use fields tool with type: "uuid" and
interface: "select-dropdown-m2o" (see fields tool <relationship_fields> M2O example)
Create relation (use relations tool):
{
"action": "create",
"collection": "articles",
"field": "author",
"data": {
"collection": "articles",
"field": "author",
"related_collection": "directus_users",
"schema": { "on_delete": "SET NULL" }
}
}
</m2o_workflow>
<o2m_workflow>
One item in a collection relates to many items in another collection.
Example: One author → many articles
Complete O2M Workflow:
Add O2M field to the "one" collection → Use fields tool with type: "alias", special: ["o2m"], and
interface: "list-o2m"
Create relation connecting to existing M2O field (use relations tool):
{
"action": "create",
"collection": "articles",
"field": "author",
"data": {
"collection": "articles",
"field": "author",
"related_collection": "authors",
"meta": {
"one_field": "articles",
"sort_field": null
},
"schema": {
"on_delete": "SET NULL"
}
}
}
</o2m_workflow>
<m2m_workflow>
Items in both collections can relate to multiple items in the other.
Example: Articles ↔ Tags
Complete M2M Workflow:
Create junction collection → Use collections tool to create article_tags with UUID primary key
Add alias fields to both collections → Use fields tool with type: "alias", special: ["m2m"], and
interface: "list-m2m" (see fields tool <relationship_fields> M2M example)
Add junction fields → Use fields tool to add article_id (UUID), tag_id (UUID), and optional sort
(integer) fields to junction collection
Create bidirectional relations (use relations tool with CASCADE):
// First relation
{
"action": "create",
"collection": "article_tags",
"field": "article_id",
"data": {
"collection": "article_tags",
"field": "article_id",
"related_collection": "articles",
"meta": {
"one_field": "tags",
"junction_field": "tag_id",
"sort_field": "sort"
},
"schema": {"on_delete": "CASCADE"}
}
}
// Second relation
{
"action": "create",
"collection": "article_tags",
"field": "tag_id",
"data": {
"collection": "article_tags",
"field": "tag_id",
"related_collection": "tags",
"meta": {
"one_field": "articles",
"junction_field": "article_id"
},
"schema": {"on_delete": "CASCADE"}
}
}
</m2m_workflow>
<m2a_workflow>
Items can relate to items from multiple different collections.
Example: Page blocks (hero, text, gallery)
Complete M2A Workflow:
Create block collections → Use collections tool to create each block type (e.g., block_hero, block_text,
block_gallery) with UUID primary keys and specific fields
Create junction collection → Use collections tool to create page_blocks junction (hidden collection with UUID
primary key)
Add M2A field → Use fields tool with type: "alias", special: ["m2a"], and interface: "list-m2a"
Add junction fields → Use fields tool to add page (UUID), item (string), collection (string), and sort
(integer) fields to junction
Create relations (use relations tool):
// Item relation (polymorphic)
{
"action": "create",
"collection": "page_blocks",
"field": "item",
"data": {
"collection": "page_blocks",
"field": "item",
"related_collection": null,
"meta": {
"one_allowed_collections": ["block_hero", "block_text", "block_gallery"],
"one_collection_field": "collection",
"junction_field": "page_id"
}
}
}
// Page relation
{
"action": "create",
"collection": "page_blocks",
"field": "page_id",
"data": {
"collection": "page_blocks",
"field": "page_id",
"related_collection": "pages",
"meta": {
"one_field": "blocks",
"junction_field": "item",
"sort_field": "sort"
},
"schema": {"on_delete": "CASCADE"}
}
}
</m2a_workflow>
<file_relationships>
Single File (M2O):
Add file field → Use fields tool with type: "uuid", special: ["file"], and interface: "file"
Create relation (use relations tool):
{
"action": "create",
"collection": "articles",
"field": "cover_image",
"data": {
"collection": "articles",
"field": "cover_image",
"related_collection": "directus_files",
"schema": { "on_delete": "SET NULL" }
}
}
Multiple Files (M2M):
Complete Files Workflow:
Create junction collection → Use collections tool to create junction with UUID primary key
Add files field to main collection → Use fields tool with type: "alias", special: ["files"], and
interface: "files" (see fields tool <relationship_fields> Files example)
Add junction fields → Use fields tool to add hidden article_id and directus_files_id (both UUID) fields to
junction
Create relations (use relations tool):
// Article relation
{
"action": "create",
"collection": "article_images",
"field": "article_id",
"data": {
"collection": "article_images",
"field": "article_id",
"related_collection": "articles",
"meta": {
"one_field": "images",
"junction_field": "directus_files_id"
},
"schema": {"on_delete": "CASCADE"}
}
}
// File relation
{
"action": "create",
"collection": "article_images",
"field": "directus_files_id",
"data": {
"collection": "article_images",
"field": "directus_files_id",
"related_collection": "directus_files",
"meta": {
"junction_field": "article_id"
},
"schema": {"on_delete": "CASCADE"}
}
}
</file_relationships>
<translations_workflow>
Special M2M relationship with languages collection.
Complete Translations Workflow:
Ensure languages collection exists (use schema tool to check)
Create translations junction → Use collections tool to create junction with UUID primary key
Add translations field → Use fields tool with type: "alias", special: ["translations"], and
interface: "translations" (see fields tool <relationship_fields> Translations example)
Configure junction fields and relations → Follow M2M pattern with languages collection </translations_workflow> </relationship_types>
<relation_settings>
on_delete:
CASCADE (default for M2M) - Delete related itemsSET NULL (default for M2O) - Set field to nullNO ACTION - Prevent deletionRESTRICT - Prevent if related items existSET DEFAULT - Set to default valueon_update:
on_deleteone_field: Field name in related collection (for O2M side)junction_field: Opposite field in junction tablesort_field: Enable manual sorting (typically an integer field)one_deselect_action: nullify or deleteone_allowed_collections: Array of collection names for M2Aone_collection_field: Field that stores collection name in M2A </relation_settings><common_patterns>
articles M2O directus_users (author)articles M2M tagsarticles M2O directus_files (cover_image)articles M2M directus_files (gallery)articles O2M commentscomments M2O directus_users (author)products M2M categoriesproducts M2O brandsproducts O2M reviewsproducts M2M directus_files (gallery)orders O2M order_itemsorder_items M2O productsorders M2O directus_users (customer)reviews M2O directus_users (reviewer)pages M2A blocks field (page_blocks junction collection)block_hero, block_text, block_gallery </common_patterns><naming_conventions>
{singular}_{plural} (e.g., product_categories)product_id, category_id)tags, categories)author, brand) </naming_conventions><related_tools>
collections: Create collections and junctions firstfields: Add relational fields before creating relationsschema: View complete relationship structure </related_tools>