import{r as s,p as c,q as h,w as n,z as e,t as l,R as t}from"./vendor.3d5b4206.js";var d="assets/m2o.ac02ed03.png",u="assets/o2m.339378e0.png",f="assets/translations.2f0017a9.png",_="assets/translations-2.29d07342.png",m="assets/m2m.07bed843.png",p="assets/m2a.347ed339.png";const y={class:"markdown-body"},g=e("blockquote",null,[e("p",null,[t("As you might have guessed, relationships are a crucial part of any relational database. Directus supports all standard relationship types, as well as a few more "),e("em",null,"compound"),t(" types that offer greater flexibility.")])],-1),w=e("p",null,[e("div",{class:"table-of-contents"},[e("ul",null,[e("li",null,[e("a",{href:"#many-to-one-(m2o)"},"Many-to-One (M2O)")]),e("li",null,[e("a",{href:"#one-to-many-(o2m)"},"One-to-Many (O2M)")]),e("li",null,[e("a",{href:"#translations-(o2m)"},"Translations (O2M)")]),e("li",null,[e("a",{href:"#many-to-many-(m2m)"},"Many-to-Many (M2M)")]),e("li",null,[e("a",{href:"#many-to-any-(m2a)"},"Many-to-Any (M2A)")]),e("li",null,[e("a",{href:"#one-to-one-(o2o)"},"One-to-One (O2O)")])])])],-1),M=e("h3",{id:"introduction"},[t("Introduction "),e("a",{class:"header-anchor",href:"#introduction"},"#")],-1),v=e("p",null,"While it may seem complex at first, relational data is actually quite straightforward once you understand what\u2019s happening behind the sometimes confusing terminology. Before diving into the details of each type, let\u2019s first cover a few basics that will help you better visualize them in your mind\u2019s eye.",-1),b=e("h3",{id:"primary-and-foreign-keys"},[t("Primary and Foreign Keys "),e("a",{class:"header-anchor",href:"#primary-and-foreign-keys"},"#")],-1),O=t("Every "),k=t("Item"),T=t(" in a relational database has a unique "),C=t("Primary Key"),x=t(" (or \u201CPK\u201D) that identifies it within its "),A=t("Collection"),j=t(". Because it\u2019s required, the key is the first "),I=t("field"),P=t(" created within a collection, typically storing an \u201Cauto-increment\u201D number, an automatically generated unique hash, or a manually entered value. They are often abbreviated to \u201CPK\u201D (Primary Key), \u201CID\u201D (Identifier), \u201CUID\u201D (Unique Identifier), or \u201CUUID\u201D (Universally Unique Identifier), depending on the type of value they store. After it\u2019s created, the value of an item\u2019s PK should "),D=e("em",null,"never",-1),S=t(" change."),R=e("p",null,[t("To link items together relationally, you simply save a reference to an item\u2019s PK in a different field. That "),e("em",null,"reference"),t(" is called a Foreign Key (FK). If the primary key is a "),e("em",null,"person"),t(", the foreign key is like their "),e("em",null,"business card"),t(". It references the actual person, providing just enough information to find them.")],-1),F=e("div",{class:"tip hint"},[e("div",{class:"hint-title"},"Composite & Compound Keys"),e("p",null,[t("In the above explanation we\u2019ve ignored "),e("em",null,"composite"),t(" and "),e("em",null,"compound"),t(" keys, which are essentially unique keys created by combining "),e("em",null,"multiple"),t(" field/column values.")])],-1),K=e("h3",{id:"perspective-matters"},[t("Perspective Matters "),e("a",{class:"header-anchor",href:"#perspective-matters"},"#")],-1),q=e("p",null,"Just like primary and foreign keys, relationships are always described relative to their \u201Cparent\u201D collection. Looking at the same relationship/key from the perspective of the opposite collection would reverse it. This means that a many-to-one relationship can also be seen as a one-to-many relationship, it just depends on which collection is considered the parent.",-1),N=e("h2",{id:"many-to-one-(m2o)"},[t("Many-to-One (M2O) "),e("a",{class:"header-anchor",href:"#many-to-one-(m2o)"},"#")],-1),J=e("p",null,[t("The Many-to-One is the only relationship type with an actual field on the parent Collection. This field saves the foreign key to a single related item. For example, a "),e("em",null,"city"),t(" can only be in one "),e("em",null,"country"),t(", but a "),e("em",null,"country"),t(" can have many "),e("em",null,"cities"),t(".")],-1),W=e("p",null,"Below is an example of a M2O relationship:",-1),B=e("pre",null,[e("code",null,`cities (the "Many" Collection)
- id (Primary Key)
- name
- country (the M2O field that stores the country key)

countries (the "One" Collection)
- id (Primary Key)
- name
`)],-1),Y=e("h3",{id:"setup"},[t("Setup "),e("a",{class:"header-anchor",href:"#setup"},"#")],-1),U=e("p",null,[e("img",{src:d,alt:"M2O"})],-1),L=e("p",null,"The parent collection and field are already known (it\u2019s the field you\u2019re currently creating), so configuring those are disabled. All you need to configure is the related collection and its primary key field.",-1),z=e("p",null,[t("If you select or enter an "),e("strong",null,"existing"),t(" Related Collection (highlights green) then the primary key field is known and automatically selected. If you enter the name of a "),e("strong",null,"new"),t(" Related Collection (doesn\u2019t already exist), you will also be prompted to enter the name of its primary key field, which will default to an auto-increment integer type.")],-1),E=e("p",null,[t("You also have the option to create a "),e("strong",null,"Corresponding Field"),t(" during this process. This allows you to more easily create the O2M alias (see below) that pairs with this M2O.")],-1),H=e("p",null,[e("strong",null,"Relational Triggers"),t(" allow for control over what happens when a relationship is broken. There is one option:")],-1),V=e("ul",null,[e("li",null,[e("strong",null,"On Delete of [Related Collection]"),t(" \u2014 When the related item (O2M) is deleted\u2026 "),e("ul",null,[e("li",null,"Nullify the parent M2O field (default)"),e("li",null,"Set the parent M2O field to its default value"),e("li",null,"Delete the parent collection\u2019s item (cascade)"),e("li",null,"Prevent the deletion")])])],-1),G=e("h2",{id:"one-to-many-(o2m)"},[t("One-to-Many (O2M) "),e("a",{class:"header-anchor",href:"#one-to-many-(o2m)"},"#")],-1),Q=t("The One-to-Many relationship is the "),X=e("strong",null,"exact same relationship",-1),Z=t(" as a Many-to-One (above), just seen from the opposite collection\u2019s perspective. The O2M is stored as an "),$=t("Alias"),ee=t(" field on its parent Collection that dynamically lists all items connected via the opposite Many-to-One. So, while the M2O will show a single country for the parent city, the O2M shows all cities for the parent country."),te=e("p",null,"Below is an example of a O2M relationship:",-1),ne=e("pre",null,[e("code",null,`countries (the "One" Collection)
- id (Primary Key)
- name
- *cities* (the O2M alias field that lists references from "cities.country")

cities (the "Many" Collection)
- id (Primary Key)
- name
- country (the M2O field that stores the country key)
`)],-1),oe=e("h3",{id:"setup-2"},[t("Setup "),e("a",{class:"header-anchor",href:"#setup-2"},"#")],-1),le=e("p",null,[e("img",{src:u,alt:"O2M"})],-1),ie=e("p",null,"The parent collection and field are already known (it\u2019s the field you\u2019re currently creating), so configuring those are disabled. All you need to configure is the related collection and its M2O field.",-1),se=e("p",null,[t("You can select or enter an "),e("strong",null,"existing"),t(" Related Collection (highlights green) or create a "),e("strong",null,"new"),t(" one by entering a unique collection name. Next, you can select an existing field (highlights green) or create a new one by entering a field name unique to its collection.")],-1),ae=e("p",null,"The related field will store the value of this collection\u2019s primary key field, so both must have the same type. For this reason, when selecting existing fields, options will be disabled if they don\u2019t have a matching type.",-1),re=e("p",null,[t("You also have the option to create a "),e("strong",null,"Corresponding Field"),t(" during this process. This allows you to more easily create the M2O field (see above) that pairs with this O2M.")],-1),ce=e("p",null,[t("The optional "),e("strong",null,"Sort Field"),t(" can be used enable manual reordering of items within this O2M field. This is configured by selecting an existing numeric type field (highlights green) from the Related Collection, or entering the name of a new field to be created.")],-1),he=e("p",null,[e("strong",null,"Relational Triggers"),t(" allow for control over what happens when a relationship is broken. There are two options:")],-1),de=e("ul",null,[e("li",null,[e("strong",null,"On Deselect of [Related Collection]"),t(" \u2014 When the value of the M2O is deselected\u2026 "),e("ul",null,[e("li",null,"Nullify the M2O field value (default)"),e("li",null,"Delete the M2O item (cascade)")])]),e("li",null,[e("strong",null,"On Delete of [Parent Collection Item]"),t(" \u2014 When this O2M item is deleted\u2026 "),e("ul",null,[e("li",null,"Nullify the related M2O field (default)"),e("li",null,"Set the related M2O field to its default value"),e("li",null,"Delete the related collection\u2019s item (cascade)"),e("li",null,"Prevent the deletion")])])],-1),ue=e("h2",{id:"translations-(o2m)"},[t("Translations (O2M) "),e("a",{class:"header-anchor",href:"#translations-(o2m)"},"#")],-1),fe=t("The Translations relationship is just a special version of the standard O2M. Just like the O2M, it creates an "),_e=t("Alias"),me=t(" field that is used to list all related items (the translations). Translations themselves are stored in a separate collection, which is then further related to "),pe=e("em",null,"third",-1),ye=t(" collection that stores all languages."),ge=e("h3",{id:"setup-3"},[t("Setup "),e("a",{class:"header-anchor",href:"#setup-3"},"#")],-1),we=e("p",null,[e("img",{src:f,alt:"Translations"})],-1),Me=e("p",null,"The easiest way to add translations is to use the wizard, which only asks for the Translation field name. All required fields and relationships will then be automatically created and configured.",-1),ve=e("p",null,[e("img",{src:_,alt:"Translations"})],-1),be=e("p",null,[t("If you choose to switch to "),e("strong",null,"manual setup"),t(", you will be presented with a similar relationship setup to O2M or M2M. The parent collection and primary key are know, so those fields are disabled.")],-1),Oe=e("p",null,"Next, we configure the Translation Collection. Set to \u201CAuto Fill\u201D by default, this will enter intelligent naming based on related names, and disables all fields. Disabling Auto Fill will enable all fields, allowing you to name the collection that holds the translations, as well as the two fields (each a M2O) that store foreign keys to the parent item and language.",-1),ke=e("p",null,[t("Lastly, you would select/create the Languages collection, which stores the languages available for this translations field. It is common practice to reuse a single languages collection throughout your project, unless translation fields need to support different language sets. For the language "),e("code",null,"code"),t(" we recommend using the IETF language tag (eg: "),e("code",null,"en-US"),t(") which combines the ISO 639-1 and ISO 3166\u20111 standards, but anything can be used (eg: "),e("code",null,"english"),t(").")],-1),Te=e("h2",{id:"many-to-many-(m2m)"},[t("Many-to-Many (M2M) "),e("a",{class:"header-anchor",href:"#many-to-many-(m2m)"},"#")],-1),Ce=t("The Many-to-Many relationship is actually just "),xe=e("strong",null,"two relationships combined",-1),Ae=t(" (O2M+M2O) that join together three different collections. The M2M is stored as an "),je=t("Alias"),Ie=t(" field on its parent Collection that dynamically lists all items connected via a "),Pe=t("junction collection"),De=t(". For example, a "),Se=e("em",null,"recipe",-1),Re=t(" can have many "),Fe=e("em",null,"ingredients",-1),Ke=t(", and "),qe=e("em",null,"ingredients",-1),Ne=t(" can be in many "),Je=e("em",null,"recipes",-1),We=t("."),Be=e("p",null,"Below is an example of a M2M relationship:",-1),Ye=e("pre",null,[e("code",null,`recipes (Collection)
- id (Primary Key)
- name
- *ingredients* (M2M/O2M alias field that lists references from "recipe_ingredients")

ingredients (Collection)
- id (Primary Key)
- name
- *recipes* (M2M/O2M alias field that lists references from "recipe_ingredients")

recipe_ingredients (Junction Collection)
- id (Primary Key)
- recipe (stores the recipe key)
- ingredient (stores ingredient key)
- quantity (other optional data on the connection)
`)],-1),Ue=e("p",null,[t("Notice that the example above also has a "),e("code",null,"quantity"),t(" field on the Junction Collection, which tracks how much of each ingredient is needed for the recipe. You can add any other "),e("em",null,"contextual"),t(" fields to the junction, and they will also be included at the top of the App\u2019s relational Item Page form.")],-1),Le=e("div",{class:"tip hint"},[e("div",{class:"hint-title"},"Self-Referencing"),e("p",null,[t("You can also have a M2M relationship that connects items within the "),e("em",null,"same"),t(" collection. An example of this is \u201CRelated Articles\u201D, where an article might relate to many other articles.")])],-1),ze=e("h3",{id:"setup-4"},[t("Setup "),e("a",{class:"header-anchor",href:"#setup-4"},"#")],-1),Ee=e("p",null,[e("img",{src:m,alt:"M2M"})],-1),He=e("p",null,"The parent collection and field are already known (it\u2019s the field you\u2019re currently creating), so configuring those are disabled.",-1),Ve=e("p",null,[t("Next you should configure the Related Collection. If you select or enter an "),e("strong",null,"existing"),t(" Related Collection (highlights green) then the primary key field is known and automatically selected. If you enter the name of a "),e("strong",null,"new"),t(" Related Collection (doesn\u2019t already exist), you will also be prompted to enter the name of its primary key field, which will default to an auto-increment integer type.")],-1),Ge=t("Lastly, we configure the "),Qe=t("Junction Collection"),Xe=t(", which sits between the two related collections, storing all links between the two. You can leave this set to \u201CAuto-Fill\u201D, which sets intelligent naming defaults, or disable it to select existing options or enter custom names."),Ze=e("p",null,[t("You also have the option to create a "),e("strong",null,"Corresponding Field"),t(" during this process. This allows you to more easily create the reverse M2M field on the "),e("em",null,"related"),t(" collection.")],-1),$e=e("p",null,[t("The optional "),e("strong",null,"Sort Field"),t(" can be used enable manual reordering of items within this O2M field. This is configured by selecting an existing numeric type field (highlights green) from the Junction Collection, or entering the name of a new field to be created.")],-1),et=e("p",null,[e("strong",null,"Relational Triggers"),t(" allow for control over what happens when a relationship is broken. There are three options:")],-1),tt=e("ul",null,[e("li",null,[e("strong",null,"On Deselect of [Junction Collection]"),t(" \u2014 When the value of this M2M is deselected\u2026 "),e("ul",null,[e("li",null,"Nullify the junction field (default)"),e("li",null,"Delete the junction item (cascade)")])]),e("li",null,[e("strong",null,"On Delete of [Parent Collection Item]"),t(" \u2014 When this M2M item is deleted\u2026 "),e("ul",null,[e("li",null,"Nullify the junction field (default)"),e("li",null,"Set the junction field to its default value"),e("li",null,"Delete the related junction item (cascade)"),e("li",null,"Prevent the deletion")])]),e("li",null,[e("strong",null,"On Delete of [Related Collection Item]"),t(" \u2014 When the related M2M item is deleted\u2026 "),e("ul",null,[e("li",null,"Nullify the junction field (default)"),e("li",null,"Set the junction field to its default value"),e("li",null,"Delete the related junction item (cascade)"),e("li",null,"Prevent the deletion")])])],-1),nt=e("h2",{id:"many-to-any-(m2a)"},[t("Many-to-Any (M2A) "),e("a",{class:"header-anchor",href:"#many-to-any-(m2a)"},"#")],-1),ot=t("Sometimes called a \u201Cmatrix field\u201D or \u201Creplicator\u201D. Like the M2M, the M2A is stored as an "),lt=t("Alias"),it=t(" field on its parent Collection that dynamically lists all items connected via a "),st=t("junction collection"),at=t(". However, there is one key difference: one side of the junction also stores a "),rt=e("strong",null,"collection key",-1),ct=t(". This combination of collection name and primary key means that you can effectively store a reference to "),ht=e("em",null,"any",-1),dt=t(" item in the database. You can then artificially limit which collections are valid through a related collections \u201Callow list\u201D."),ut=e("p",null,[t("A common example of a M2A is a \u201CPage Builder\u201D, which has a "),e("em",null,"Pages"),t(" collection that includes any number of different \u201Csection\u201D Collections, such as: \u201CHeading\u201D, \u201CText\u201D, and \u201CImage\u201D. In this example the junction table will link different sections (from different collections) with a page, creating relational layouts.")],-1),ft=e("p",null,"Below is an example of a M2A relationship:",-1),_t=e("pre",null,[e("code",null,`pages (Collection)
- id
- name
- *sections* (O2M/M2A alias field that lists references from "sections")

page_sections (Junction Collection)
- id
- pages_id (stores the primary key of the parent page)
- collection (stores name of the related collection, for example "headings", "text", or "images")
- item (stores the primary key of the related item)

headings (Collection)
- id
- title

text (Collection)
- id
- text

images (Collection)
- id
- file
`)],-1),mt=e("h3",{id:"setup-5"},[t("Setup "),e("a",{class:"header-anchor",href:"#setup-5"},"#")],-1),pt=e("p",null,[e("img",{src:p,alt:"M2A"})],-1),yt=e("p",null,"The parent collection and field are already known (it\u2019s the field you\u2019re currently creating), so configuring those are disabled.",-1),gt=t("Next, we configure the "),wt=t("Junction Collection"),Mt=t(", which sits between the related collections, storing all links between them. You can leave this set to \u201CAuto-Fill\u201D, which sets intelligent naming defaults, or disable it to select existing options or enter custom names."),vt=e("p",null,[t("Lastly, you should select any desired Related Collections. Unlike other relationships, you can\u2019t "),e("em",null,"create"),t(" these related collections here, so ensure all related collections you need are created before hand.")],-1),bt=e("div",{class:"tip hint"},[e("div",{class:"hint-title"},"Auto-generating"),e("p",null,"If you enter a collection/field name that doesn\u2019t exist yet, Directus will auto-generate these collections/fields once you save the changes on the new M2A field.")],-1),Ot=e("p",null,[t("The optional "),e("strong",null,"Sort Field"),t(" can be used enable manual reordering of items within this M2A field. This is configured by selecting an existing numeric type field (highlights green) from the Junction Collection, or entering the name of a new field to be created.")],-1),kt=e("p",null,[e("strong",null,"Relational Triggers"),t(" allow for control over what happens when a relationship is broken. There are three options:")],-1),Tt=e("ul",null,[e("li",null,[e("strong",null,"On Delete of [Parent Collection Item]"),t(" \u2014 When a M2A item is deleted\u2026 "),e("ul",null,[e("li",null,"Nullify the junction field (default)"),e("li",null,"Set the junction field to its default value"),e("li",null,"Delete the related junction item (cascade)"),e("li",null,"Prevent the deletion")])]),e("li",null,[e("strong",null,"On Deselect of [Junction Collection]"),t(" \u2014 When the value of this M2A is deselected\u2026 "),e("ul",null,[e("li",null,"Nullify the junction field (default)"),e("li",null,"Delete the junction item (cascade)")])])],-1),Ct=e("h2",{id:"one-to-one-(o2o)"},[t("One-to-One (O2O) "),e("a",{class:"header-anchor",href:"#one-to-one-(o2o)"},"#")],-1),xt=t("Directus does not include a dedicated One-to-One (O2O) relationship type or interface. However, O2O is essentially the same as a M2O (storing a foreign key). The only difference is that a O2O enforces the cardinality. In other words, selecting a relational item in a O2O means that item can not be selected elsewhere (it can only be used once). This functionality can be added by checking and constraining uniqueness via a "),At=t("custom event hook"),jt=t(" or "),It=t("custom interface"),Pt=t("."),Dt=e("p",null,[t("An example of this is a "),e("em",null,"person"),t(" only has one unique set of "),e("em",null,"fingerprints"),t(", and those "),e("em",null,"fingerprints"),t(" only belong to one "),e("em",null,"person"),t(".")],-1),qt={setup(St,{expose:a}){const i={title:"Relationships",modularExtension:!1};return a({frontmatter:i}),(Rt,Ft)=>{const o=s("router-link"),r=s("DocsWrapper");return c(),h(r,{frontmatter:i},{default:n(()=>[e("div",y,[g,w,M,v,b,e("p",null,[O,l(o,{to:"/docs/getting-started/glossary/#items"},{default:n(()=>[k]),_:1}),T,l(o,{to:"/docs/getting-started/glossary/#primary-key-pk"},{default:n(()=>[C]),_:1}),x,l(o,{to:"/docs/getting-started/glossary/#collections"},{default:n(()=>[A]),_:1}),j,l(o,{to:"/docs/getting-started/glossary/#fields"},{default:n(()=>[I]),_:1}),P,D,S]),R,F,K,q,N,J,W,B,Y,U,L,z,E,H,V,G,e("p",null,[Q,X,Z,l(o,{to:"/docs/getting-started/glossary/#alias"},{default:n(()=>[$]),_:1}),ee]),te,ne,oe,le,ie,se,ae,re,ce,he,de,ue,e("p",null,[fe,l(o,{to:"/docs/getting-started/glossary/#alias"},{default:n(()=>[_e]),_:1}),me,pe,ye]),ge,we,Me,ve,be,Oe,ke,Te,e("p",null,[Ce,xe,Ae,l(o,{to:"/docs/getting-started/glossary/#alias"},{default:n(()=>[je]),_:1}),Ie,l(o,{to:"/docs/getting-started/glossary/#junction-collections"},{default:n(()=>[Pe]),_:1}),De,Se,Re,Fe,Ke,qe,Ne,Je,We]),Be,Ye,Ue,Le,ze,Ee,He,Ve,e("p",null,[Ge,l(o,{to:"/docs/getting-started/glossary/#junction-collections"},{default:n(()=>[Qe]),_:1}),Xe]),Ze,$e,et,tt,nt,e("p",null,[ot,l(o,{to:"/docs/getting-started/glossary/#alias"},{default:n(()=>[lt]),_:1}),it,l(o,{to:"/docs/getting-started/glossary/#junction-collections"},{default:n(()=>[st]),_:1}),at,rt,ct,ht,dt]),ut,ft,_t,mt,pt,yt,e("p",null,[gt,l(o,{to:"/docs/getting-started/glossary/#junction-collections"},{default:n(()=>[wt]),_:1}),Mt]),vt,bt,Ot,kt,Tt,Ct,e("p",null,[xt,l(o,{to:"/docs/extensions/hooks/"},{default:n(()=>[At]),_:1}),jt,l(o,{to:"/docs/extensions/interfaces/"},{default:n(()=>[It]),_:1}),Pt]),Dt])]),_:1})}}};export{qt as default};
