Files
directus/docs/reference/api/rest/relations.md
Rijk van Zanten 817ccf3620 Overhaul docs (#3951)
* Add Quickstart Guide

* Update installation

* Remove unused files

* Update support/backing

* Tweaks in concepts

* Setup file structure for API reference 2.0

* Setup page layout for reference

* Add clean-urls plugin

* getting started updates

* Finish authentication rest

* getting started updates

* Render stylus in 2 spaces

* Various

* Various

* Finish activity docs

* Add collections reference

* Add extension reference

* concepts updates

* Fields/tweaks

* Add files doc

* Add revisions

* concepts docs

* More api reference

* Finish rest api reference (finally)

* initial concepts

* More things

* Add assets api ref

* Move sections from file to assets

* Add environment variables

* contributing docs

* Add field transforms page

* Left align table headers

* concept links

* Add API config

* Fix mobile nav

* Add migrating a project

* doc link fixes

Co-authored-by: Ben Haynes <ben@rngr.org>
2021-02-05 18:51:54 -05:00

5.1 KiB

pageClass
pageClass
page-reference

Relations

What data is linked to what other data. Allows you to assign authors to articles, products to sales, and whatever other structures you can think of.

toc


The Relation Object

id integer
Primary key of the relation.

many_collection string
Collection on the "many" side of the relation.

many_field string
Field on the "many" side of the relation.

one_collection string
Collection on the "one" side of the relation.

one_field string
Field on the "one" side of the relation.

one_collection_field string
In Many-to-Any type fields, this holds the field in the many collection that holds the name of the "one" collection.

one_allowed_collections string
In Many-to-Any type fields, this holds a csv of collection names the user is allowed to use through the m2a relation.

junction_field string
For Many-to-Many type fields, this holds the name of the field that "links" a many-to-one to a one-to-many, creating a many-to-many.

{
	"id": 13,
	"many_collection": "articles",
	"many_field": "featured_image",
	"one_collection": "directus_files",
	"one_field": null,
	"one_collection_field": null,
	"one_allowed_collections": null,
	"junction_field": null
}

List relations

List all relations that exist in Directus.

::: tip Permissions

The data returned in this endpoint will be filtered based on the user's permissions. For example, relations that apply to a collection that the current user doesn't have access to are stripped out.

:::

Query Parameters

Supports all global query parameters.

Returns

An array of up to limit relation objects. If no items are available, data will be an empty array.

GET /relations

// Response

{
	"data": [
		{
			"id": 13,
			"many_collection": "articles",
			"many_field": "featured_image",
			"one_collection": "directus_files",
			"one_field": null,
			"one_collection_field": null,
			"one_allowed_collections": null,
			"junction_field": null
		},
		{...},
		{...}
	]
}

Retrieve a relation

List an existing relation by primary key.

Query Parameters

Supports all global query parameters.

Returns

Returns the requested relation object.

GET /relations/:id

// Response

{
	"data": {
		"id": 13,
		"many_collection": "articles",
		"many_field": "featured_image",
		"one_collection": "directus_files",
		"one_field": null,
		"one_collection_field": null,
		"one_allowed_collections": null,
		"junction_field": null
	}
}

Create relation

Create one or more new relation(s).

Query Parameters

Supports all global query parameters.

Request Body

A partial relation object or an array of partial relation objects.

Returns

Returns the relation object for the created relation.

POST /relations

// Request

{
	"many_collection": "articles",
	"many_field": "featured_image",
	"one_collection": "directus_files"
}
// Response

{
	"data": {
		"id": 13,
		"many_collection": "articles",
		"many_field": "featured_image",
		"one_collection": "directus_files",
		"one_field": null,
		"one_collection_field": null,
		"one_allowed_collections": null,
		"junction_field": null
	}
}

Update relation

Update an existing relation.

Query Parameters

Supports all global query parameters.

Request Body

A partial relation object.

Returns

Returns the relation object for the created relation.

PATCH /relations/:id

// Request

{
	"one_field": "articles"
}
// Response

{
	"data": {
		"id": 13,
		"many_collection": "articles",
		"many_field": "featured_image",
		"one_collection": "directus_files",
		"one_field": "articles",
		"one_collection_field": null,
		"one_allowed_collections": null,
		"junction_field": null
	}
}

Delete relation

Delete an existing relation.

Returns

Empty body.

DELETE /relations/:id

// Empty Response

Delete Multiple relations

Delete multiple existing relations.

Request Body

An array of relation primary keys

Returns

Empty body.

DELETE /relations

// Request
[15, 251, 810]
// Empty Response