import{r as s,p as i,q as c,w as t,z as e,t as h,R as l}from"./vendor.8ed37afc.js";const d={class:"markdown-body"},p={class:"two-up"},u={class:"left"},_=l("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. "),g=l("Learn more about Relationships"),f=l("."),m=e("div",{class:"right"},[e("p",null,[e("div",{class:"table-of-contents"},[e("ul",null,[e("li",null,[e("a",{href:"#the-relation-object"},"The Relation Object")]),e("li",null,[e("a",{href:"#list-relations"},"List relations")]),e("li",null,[e("a",{href:"#list-relations-in-collection"},"List relations in collection")]),e("li",null,[e("a",{href:"#retrieve-a-relation"},"Retrieve a relation")]),e("li",null,[e("a",{href:"#create-a-relation"},"Create a Relation")]),e("li",null,[e("a",{href:"#update-a-relation"},"Update a Relation")]),e("li",null,[e("a",{href:"#delete-a-relation"},"Delete a Relation")])])])])],-1),y=e("hr",null,null,-1),j=e("h2",{id:"the-relation-object"},[l("The Relation Object "),e("a",{class:"header-anchor",href:"#the-relation-object"},"#")],-1),b=e("div",{class:"two-up"},[e("div",{class:"left"},[e("div",{class:"definitions"},[e("p",null,[e("code",null,"collection"),l(),e("strong",null,"string"),e("br"),l(" Name of the collection. This matches the table name in the database.")]),e("p",null,[e("code",null,"field"),l(),e("strong",null,"string"),e("br"),l(" Name of the field that holds the related primary key. This matches the column name in the database.")]),e("p",null,[e("code",null,"related_collection"),l(),e("strong",null,"string"),e("br"),l(" Name of the related collection. This matches the table name in the database.")])]),e("h4",{id:"meta"},[l("Meta "),e("a",{class:"header-anchor",href:"#meta"},"#")]),e("p",null,"Directus metadata. Used to enable non-database relationship types"),e("div",{class:"definitions"},[e("p",null,[e("code",null,"id"),l(),e("strong",null,"integer"),e("br"),l(" Primary key of the metadata row in "),e("code",null,"directus_relations"),l(".")]),e("p",null,[e("code",null,"many_collection"),l(),e("strong",null,"string"),e("br"),l(" Name of the collection. Matches the top level "),e("code",null,"collection"),l(" field.")]),e("p",null,[e("code",null,"many_field"),l(),e("strong",null,"string"),e("br"),l(" Name of the field. Matches the top level "),e("code",null,"field"),l(" field.")]),e("p",null,[e("code",null,"one_collection"),l(),e("strong",null,"string"),e("br"),l(" Name of the related collection. Matches the top level "),e("code",null,"related_collection"),l(" field.")]),e("p",null,[e("code",null,"one_field"),l(),e("strong",null,"string"),e("br"),l(" Name of the one to many field on the other side of the relation.")]),e("p",null,[e("code",null,"one_allowed_collections"),l(),e("strong",null,"string"),e("br"),l(" What collections are allowed to be used in a many-to-any context.")]),e("p",null,[e("code",null,"one_collection_field"),l(),e("strong",null,"string"),e("br"),l(" Field that holds the collection name in a many-to-any context.")]),e("p",null,[e("code",null,"one_deselect_action"),l(),e("strong",null,"nullify | delete"),e("br"),l(" Whether to nullify or delete related one-to-many records.")]),e("p",null,[e("code",null,"sort_field"),l(),e("strong",null,"string"),e("br"),l(" What field is used to hold the sort field.")]),e("p",null,[e("code",null,"junction_field"),l(),e("strong",null,"string"),e("br"),l(" What field connects two relations in a many-to-many (o2m-m2o) context.")])]),e("h4",{id:"schema"},[l("Schema "),e("a",{class:"header-anchor",href:"#schema"},"#")]),e("p",null,"\u201CRaw\u201D database information. Based on the database vendor used, different information might be returned. The following are available for all drivers."),e("div",{class:"definitions"},[e("p",null,[e("code",null,"table"),l(),e("strong",null,"string"),e("br"),l(" The table name.")]),e("p",null,[e("code",null,"column"),l(),e("strong",null,"string"),e("br"),l(" The column name.")]),e("p",null,[e("code",null,"foreign_key_table"),l(),e("strong",null,"string"),e("br"),l(" Related table name.")]),e("p",null,[e("code",null,"foreign_key_column"),l(),e("strong",null,"string"),e("br"),l(" Related column name.")]),e("p",null,[e("code",null,"constraint_name"),l(),e("strong",null,"string"),e("br"),l(" Name for the foreign key constraint.")]),e("p",null,[e("code",null,"on_update"),l(),e("strong",null,"string"),e("br"),l(" Update trigger for the foreign key constraint.")]),e("p",null,[e("code",null,"on_delete"),l(),e("strong",null,"string"),e("br"),l(" Delete trigger for the foreign key constraint.")])])]),e("div",{class:"right"},[e("pre",null,[e("code",{class:"language-json"},[l(`{
	`),e("span",{class:"hljs-attr"},'"collection"'),l(": "),e("span",{class:"hljs-string"},'"about_us"'),l(`,
	`),e("span",{class:"hljs-attr"},'"field"'),l(": "),e("span",{class:"hljs-string"},'"logo"'),l(`,
	`),e("span",{class:"hljs-attr"},'"related_collection"'),l(": "),e("span",{class:"hljs-string"},'"directus_files"'),l(`,
	`),e("span",{class:"hljs-attr"},'"schema"'),l(`: {
		`),e("span",{class:"hljs-attr"},'"table"'),l(": "),e("span",{class:"hljs-string"},'"about_us"'),l(`,
		`),e("span",{class:"hljs-attr"},'"column"'),l(": "),e("span",{class:"hljs-string"},'"logo"'),l(`,
		`),e("span",{class:"hljs-attr"},'"foreign_key_table"'),l(": "),e("span",{class:"hljs-string"},'"directus_files"'),l(`,
		`),e("span",{class:"hljs-attr"},'"foreign_key_column"'),l(": "),e("span",{class:"hljs-string"},'"id"'),l(`,
		`),e("span",{class:"hljs-attr"},'"constraint_name"'),l(": "),e("span",{class:"hljs-string"},'"about_us_logo_foreign"'),l(`,
		`),e("span",{class:"hljs-attr"},'"on_update"'),l(": "),e("span",{class:"hljs-string"},'"NO ACTION"'),l(`,
		`),e("span",{class:"hljs-attr"},'"on_delete"'),l(": "),e("span",{class:"hljs-string"},'"SET NULL"'),l(`
	},
	`),e("span",{class:"hljs-attr"},'"meta"'),l(`: {
		`),e("span",{class:"hljs-attr"},'"id"'),l(": "),e("span",{class:"hljs-number"},"1"),l(`,
		`),e("span",{class:"hljs-attr"},'"junction_field"'),l(": "),e("span",{class:"hljs-literal"},"null"),l(`,
		`),e("span",{class:"hljs-attr"},'"many_collection"'),l(": "),e("span",{class:"hljs-string"},'"about_us"'),l(`,
		`),e("span",{class:"hljs-attr"},'"many_field"'),l(": "),e("span",{class:"hljs-string"},'"logo"'),l(`,
		`),e("span",{class:"hljs-attr"},'"one_allowed_collections"'),l(": "),e("span",{class:"hljs-literal"},"null"),l(`,
		`),e("span",{class:"hljs-attr"},'"one_collection"'),l(": "),e("span",{class:"hljs-string"},'"directus_files"'),l(`,
		`),e("span",{class:"hljs-attr"},'"one_collection_field"'),l(": "),e("span",{class:"hljs-literal"},"null"),l(`,
		`),e("span",{class:"hljs-attr"},'"one_deselect_action"'),l(": "),e("span",{class:"hljs-string"},'"nullify"'),l(`,
		`),e("span",{class:"hljs-attr"},'"one_field"'),l(": "),e("span",{class:"hljs-literal"},"null"),l(`,
		`),e("span",{class:"hljs-attr"},'"sort_field"'),l(": "),e("span",{class:"hljs-literal"},"null"),l(`
	}
}
`)])])])],-1),q=e("hr",null,null,-1),v=e("h2",{id:"list-relations"},[l("List relations "),e("a",{class:"header-anchor",href:"#list-relations"},"#")],-1),x=e("p",null,"List all relations that exist in Directus.",-1),w=e("div",{class:"two-up"},[e("div",{class:"left"},[e("div",{class:"tip hint"},[e("div",{class:"hint-title"},"Permissions"),e("p",null,"The data returned in this endpoint will be filtered based on the user\u2019s permissions. For example, relations that apply to a collection that the current user doesn\u2019t have access to are stripped out.")]),e("h3",{id:"query-parameters"},[l("Query Parameters "),e("a",{class:"header-anchor",href:"#query-parameters"},"#")]),e("p",null,"Doesn\u2019t support any query parameters."),e("h3",{id:"returns"},[l("Returns "),e("a",{class:"header-anchor",href:"#returns"},"#")]),e("p",null,[l("Array of "),e("a",{href:"#the-relation-object"},"relation objects"),l(". If no items are available, data will be an empty array.")])]),e("div",{class:"right"},[e("h3",{id:"rest-api"},[l("REST API "),e("a",{class:"header-anchor",href:"#rest-api"},"#")]),e("pre",null,[e("code",null,`GET /relations
`)]),e("h3",{id:"graphql"},[l("GraphQL "),e("a",{class:"header-anchor",href:"#graphql"},"#")]),e("pre",null,[e("code",null,`POST /graphql/system
`)]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"type"),e("span",{class:"hljs-type"}," Query"),l(` {
	relations: [directus_relations]
}
`)])]),e("h5",{id:"example"},[l("Example "),e("a",{class:"header-anchor",href:"#example"},"#")]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"query"),l(` {
	relations {
		collection
		field
	}
}
`)])])])],-1),T=e("hr",null,null,-1),R=e("h2",{id:"list-relations-in-collection"},[l("List relations in collection "),e("a",{class:"header-anchor",href:"#list-relations-in-collection"},"#")],-1),E=e("p",null,"List all relations that exist in a given collection.",-1),k=e("div",{class:"two-up"},[e("div",{class:"left"},[e("div",{class:"tip hint"},[e("div",{class:"hint-title"},"Permissions"),e("p",null,"The data returned in this endpoint will be filtered based on the user\u2019s permissions. For example, relations that apply to a collection that the current user doesn\u2019t have access to are stripped out.")]),e("h3",{id:"query-parameters-2"},[l("Query Parameters "),e("a",{class:"header-anchor",href:"#query-parameters-2"},"#")]),e("p",null,"Doesn\u2019t support any query parameters."),e("h3",{id:"returns-2"},[l("Returns "),e("a",{class:"header-anchor",href:"#returns-2"},"#")]),e("p",null,[l("Array of "),e("a",{href:"#the-relation-object"},"relation objects"),l(". If no items are available, data will be an empty array.")])]),e("div",{class:"right"},[e("h3",{id:"rest-api-2"},[l("REST API "),e("a",{class:"header-anchor",href:"#rest-api-2"},"#")]),e("pre",null,[e("code",null,`GET /relations/:collection
`)]),e("h5",{id:"example-2"},[l("Example "),e("a",{class:"header-anchor",href:"#example-2"},"#")]),e("pre",null,[e("code",null,`GET /relations/articles
`)]),e("h3",{id:"graphql-2"},[l("GraphQL "),e("a",{class:"header-anchor",href:"#graphql-2"},"#")]),e("pre",null,[e("code",null,`POST /graphql/system
`)]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"type"),e("span",{class:"hljs-type"}," Query"),l(` {
	relations_in_collection(collection:`),e("span",{class:"hljs-type"}," String"),l(`!): [directus_relations]
}
`)])]),e("h5",{id:"example-3"},[l("Example "),e("a",{class:"header-anchor",href:"#example-3"},"#")]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"query"),l(` {
	relations_in_collection(collection: `),e("span",{class:"hljs-string"},'"articles"'),l(`) {
		collection
		field
	}
}
`)])])])],-1),P=e("hr",null,null,-1),S=e("h2",{id:"retrieve-a-relation"},[l("Retrieve a relation "),e("a",{class:"header-anchor",href:"#retrieve-a-relation"},"#")],-1),L=e("p",null,"List an existing relation by collection/field name.",-1),D=e("div",{class:"two-up"},[e("div",{class:"left"},[e("h3",{id:"query-parameters-3"},[l("Query Parameters "),e("a",{class:"header-anchor",href:"#query-parameters-3"},"#")]),e("p",null,"Doesn\u2019t support any query parameters."),e("h3",{id:"returns-3"},[l("Returns "),e("a",{class:"header-anchor",href:"#returns-3"},"#")]),e("p",null,[l("Returns the requested "),e("a",{href:"#the-relation-object"},"relation object"),l(".")])]),e("div",{class:"right"},[e("h3",{id:"rest-api-3"},[l("REST API "),e("a",{class:"header-anchor",href:"#rest-api-3"},"#")]),e("pre",null,[e("code",null,`GET /relations/:collection/:field
`)]),e("h5",{id:"example-4"},[l("Example "),e("a",{class:"header-anchor",href:"#example-4"},"#")]),e("pre",null,[e("code",null,`GET /relations/articles/featured_image
`)]),e("h3",{id:"graphql-3"},[l("GraphQL "),e("a",{class:"header-anchor",href:"#graphql-3"},"#")]),e("pre",null,[e("code",null,`POST /graphql/system
`)]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"type"),e("span",{class:"hljs-type"}," Query"),l(` {
	relations_by_name(collection:`),e("span",{class:"hljs-type"}," String"),l("!, field:"),e("span",{class:"hljs-type"}," String"),l(`!): directus_relations
}
`)])]),e("h5",{id:"example-5"},[l("Example "),e("a",{class:"header-anchor",href:"#example-5"},"#")]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"query"),l(` {
	relations_by_name(collection: `),e("span",{class:"hljs-string"},'"articles"'),l(", field: "),e("span",{class:"hljs-string"},'"featured_image"'),l(`) {
		collection
		field
		related_collection
	}
}
`)])])])],-1),A=e("hr",null,null,-1),N=e("h2",{id:"create-a-relation"},[l("Create a Relation "),e("a",{class:"header-anchor",href:"#create-a-relation"},"#")],-1),Q=e("p",null,"Create a new relation.",-1),O=e("div",{class:"two-up"},[e("div",{class:"left"},[e("h3",{id:"query-parameters-4"},[l("Query Parameters "),e("a",{class:"header-anchor",href:"#query-parameters-4"},"#")]),e("p",null,"Doesn\u2019t support any query parameters."),e("h3",{id:"request-body"},[l("Request Body "),e("a",{class:"header-anchor",href:"#request-body"},"#")]),e("p",null,[l("A partial "),e("a",{href:"#the-relation-object"},"relation object"),l(".")]),e("h3",{id:"returns-4"},[l("Returns "),e("a",{class:"header-anchor",href:"#returns-4"},"#")]),e("p",null,[l("Returns the "),e("a",{href:"#the-relation-object"},"relation object"),l(" for the created relation.")])]),e("div",{class:"right"},[e("h3",{id:"rest-api-4"},[l("REST API "),e("a",{class:"header-anchor",href:"#rest-api-4"},"#")]),e("pre",null,[e("code",null,`POST /relations
`)]),e("h5",{id:"example-6"},[l("Example "),e("a",{class:"header-anchor",href:"#example-6"},"#")]),e("pre",null,[e("code",{class:"language-json"},[e("span",{class:"hljs-comment"},"// POST /relations"),l(`

{
	`),e("span",{class:"hljs-attr"},'"collection"'),l(": "),e("span",{class:"hljs-string"},'"articles"'),l(`,
	`),e("span",{class:"hljs-attr"},'"field"'),l(": "),e("span",{class:"hljs-string"},'"featured_image"'),l(`,
	`),e("span",{class:"hljs-attr"},'"related_collection"'),l(": "),e("span",{class:"hljs-string"},'"directus_files"'),l(`
}
`)])]),e("h3",{id:"graphql-4"},[l("GraphQL "),e("a",{class:"header-anchor",href:"#graphql-4"},"#")]),e("pre",null,[e("code",null,`POST /graphql/system
`)]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"type"),e("span",{class:"hljs-type"}," Mutation"),l(` {
	create_relations_item(data: create_directus_relations_input!): directus_relations
}
`)])]),e("h5",{id:"example-7"},[l("Example "),e("a",{class:"header-anchor",href:"#example-7"},"#")]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"mutation"),l(` {
	create_relations_item(
		data: { collection: `),e("span",{class:"hljs-string"},'"articles"'),l(", field: "),e("span",{class:"hljs-string"},'"featured_image"'),l(", related_collection: "),e("span",{class:"hljs-string"},'"directus_files"'),l(` }
	) {
		collection
		field
		related_collection
	}
}
`)])])])],-1),G=e("hr",null,null,-1),C=e("h2",{id:"update-a-relation"},[l("Update a Relation "),e("a",{class:"header-anchor",href:"#update-a-relation"},"#")],-1),I=e("p",null,"Update an existing relation.",-1),M=e("div",{class:"two-up"},[e("div",{class:"left"},[e("h3",{id:"query-parameters-5"},[l("Query Parameters "),e("a",{class:"header-anchor",href:"#query-parameters-5"},"#")]),e("p",null,"Doesn\u2019t support any query parameters."),e("h3",{id:"request-body-2"},[l("Request Body "),e("a",{class:"header-anchor",href:"#request-body-2"},"#")]),e("p",null,[l("A partial "),e("a",{href:"#the-relation-object"},"relation object"),l(".")]),e("h3",{id:"returns-5"},[l("Returns "),e("a",{class:"header-anchor",href:"#returns-5"},"#")]),e("p",null,[l("Returns the "),e("a",{href:"#the-relation-object"},"relation object"),l(" for the created relation.")])]),e("div",{class:"right"},[e("h3",{id:"rest-api-5"},[l("REST API "),e("a",{class:"header-anchor",href:"#rest-api-5"},"#")]),e("pre",null,[e("code",null,`PATCH /relations/:collection/:field
`)]),e("h5",{id:"example-8"},[l("Example "),e("a",{class:"header-anchor",href:"#example-8"},"#")]),e("pre",null,[e("code",{class:"language-json"},[e("span",{class:"hljs-comment"},"// PATCH /relations/articles/author"),l(`

{
	`),e("span",{class:"hljs-attr"},'"meta"'),l(`: {
		`),e("span",{class:"hljs-attr"},'"one_field"'),l(": "),e("span",{class:"hljs-string"},'"articles"'),l(`
	}
}
`)])]),e("h3",{id:"graphql-5"},[l("GraphQL "),e("a",{class:"header-anchor",href:"#graphql-5"},"#")]),e("pre",null,[e("code",null,`POST /graphql/system
`)]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"type"),e("span",{class:"hljs-type"}," Mutation"),l(` {
	update_relations_item(collection:`),e("span",{class:"hljs-type"}," String"),l("!, field:"),e("span",{class:"hljs-type"}," String"),l(`!, data: update_directus_relations_input!): directus_relations
}
`)])]),e("h5",{id:"example-9"},[l("Example "),e("a",{class:"header-anchor",href:"#example-9"},"#")]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"mutation"),l(` {
	update_relations_item(collection: `),e("span",{class:"hljs-string"},'"articles"'),l(", field: "),e("span",{class:"hljs-string"},'"author"'),l(", data: { meta: { one_field: "),e("span",{class:"hljs-string"},'"articles"'),l(` } }) {
		collection
		field
		related_collection
	}
}
`)])])])],-1),W=e("hr",null,null,-1),B=e("h2",{id:"delete-a-relation"},[l("Delete a Relation "),e("a",{class:"header-anchor",href:"#delete-a-relation"},"#")],-1),U=e("p",null,"Delete an existing relation.",-1),F=e("div",{class:"two-up"},[e("div",{class:"left"},[e("h3",{id:"returns-6"},[l("Returns "),e("a",{class:"header-anchor",href:"#returns-6"},"#")]),e("p",null,"Empty body.")]),e("div",{class:"right"},[e("h3",{id:"rest-api-6"},[l("REST API "),e("a",{class:"header-anchor",href:"#rest-api-6"},"#")]),e("pre",null,[e("code",null,`DELETE /relations/:collection/:field
`)]),e("h5",{id:"example-10"},[l("Example "),e("a",{class:"header-anchor",href:"#example-10"},"#")]),e("pre",null,[e("code",null,`DELETE /relations/articles/author
`)]),e("h3",{id:"graphql-6"},[l("GraphQL "),e("a",{class:"header-anchor",href:"#graphql-6"},"#")]),e("pre",null,[e("code",null,`POST /graphql/system
`)]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"type"),e("span",{class:"hljs-type"}," Mutation"),l(` {
	delete_relations_item(collection:`),e("span",{class:"hljs-type"}," String"),l("!, field:"),e("span",{class:"hljs-type"}," String"),l(`!): delete_one
}
`)])]),e("h5",{id:"example-11"},[l("Example "),e("a",{class:"header-anchor",href:"#example-11"},"#")]),e("pre",null,[e("code",{class:"language-graphql"},[e("span",{class:"hljs-keyword"},"mutation"),l(` {
	delete_relations_item(collection: `),e("span",{class:"hljs-string"},'"articles"'),l(", field: "),e("span",{class:"hljs-string"},'"author"'),l(`) {
		collection
		field
	}
}
`)])])])],-1),K={setup(V,{expose:n}){const a={title:"Relations",modularExtension:!1,pageClass:"page-reference"};return n({frontmatter:a}),(H,z)=>{const r=s("router-link"),o=s("DocsWrapper");return i(),c(o,{frontmatter:a},{default:t(()=>[e("div",d,[e("div",p,[e("div",u,[e("blockquote",null,[e("p",null,[_,h(r,{to:"/docs/getting-started/glossary/#relationships"},{default:t(()=>[g]),_:1}),f])])]),m]),y,j,b,q,v,x,w,T,R,E,k,P,S,L,D,A,N,Q,O,G,C,I,M,W,B,U,F])]),_:1})}}};export{K as default};
