<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged sql-relations</title>
<link>https://coderlegion.com/tag/sql-relations</link>
<description>Powered by Question2Answer</description>
<item>
<title>Directus Basics Part 2 — Understanding Relationships (M2O, O2M, M2M)</title>
<link>https://coderlegion.com/24803/directus-basics-part-2-understanding-relationships-m2o-o2m-m2m</link>
<description>&lt;p&gt;This is part two of our Directus Basics series. In &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.youtube.com/watch?v=tJGuxqhv2SY&amp;amp;t=6s&quot;&gt;part one&lt;/a&gt;, we set up a Directus instance and created our first collection. Today we're covering one of the most powerful features Directus offers: &lt;strong&gt;relationships&lt;/strong&gt; between collections.&lt;/p&gt;
&lt;p&gt;By the end of this article, you'll understand three core relationship types — Many to One (M2O), One to Many (O2M), and Many to Many (M2M). There's a fourth type, Many to Any (M2A), which we'll cover in a future post.&lt;/p&gt;
&lt;h3 id=&quot;setting-up-the-branding-collection&quot;&gt;Setting Up the Branding Collection&lt;/h3&gt;
&lt;p&gt;Continuing from our existing Directus instance, let's create a new collection called &lt;strong&gt;branding&lt;/strong&gt;. Select all the additional default fields Directus offers.&lt;/p&gt;
&lt;p&gt;Add two fields:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Field &lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;name&lt;/code&gt; &lt;/td&gt;&lt;td&gt;String (basic input)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;description&lt;/code&gt; &lt;/td&gt;&lt;td&gt;Text (textarea)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Once the collection is set up, add a few entries — I'd recommend at least two different brands so you can see the relationship in action once it's wired up.&lt;/p&gt;
&lt;h3 id=&quot;many-to-one-m2o-products-branding&quot;&gt;Many to One (M2O): Products → Branding&lt;/h3&gt;
&lt;p&gt;Head over to your &lt;strong&gt;products&lt;/strong&gt; collection. We want to connect a product to a brand.&lt;/p&gt;
&lt;p&gt;Logically: many products can belong to &lt;em&gt;one&lt;/em&gt; brand. That's a Many to One relationship — many products, to one brand.&lt;/p&gt;
&lt;p&gt;Steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create Field → Relational category → &lt;strong&gt;Many to One&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Key: &lt;code&gt;brand_id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Related Collection: &lt;code&gt;branding&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;De-select &lt;strong&gt;Enable Create Button&lt;/strong&gt; — you always want to choose a brand from an existing list, not create one on the fly&lt;/li&gt;
&lt;li&gt;Save&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;one-to-many-o2m-branding-products&quot;&gt;One to Many (O2M): Branding → Products&lt;/h3&gt;
&lt;p&gt;Since one brand can have &lt;em&gt;many&lt;/em&gt; products, we need to set up the reverse relationship explicitly on the &lt;strong&gt;branding&lt;/strong&gt; collection.&lt;/p&gt;
&lt;p&gt;Steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create Field → Relational category → &lt;strong&gt;One to Many&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Key: &lt;code&gt;products&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Related Collection: &lt;code&gt;products&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Foreign Key: &lt;code&gt;brand_id&lt;/code&gt; — the field we just created on the products side&lt;/li&gt;
&lt;li&gt;De-select &lt;strong&gt;Enable Create Button&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Save&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;seeing-the-relationship-in-action&quot;&gt;Seeing the Relationship in Action&lt;/h3&gt;
&lt;p&gt;Go back into your &lt;strong&gt;products&lt;/strong&gt; collection and assign a brand to each product. Make sure at least two products share the same brand so the relationship is visible.&lt;/p&gt;
&lt;p&gt;You'll notice each product can only belong to &lt;strong&gt;one&lt;/strong&gt; brand — that's the M2O relationship at work.&lt;/p&gt;
&lt;p&gt;Now open the &lt;strong&gt;branding&lt;/strong&gt; collection and click into one of your brands. You'll see the products you just assigned show up automatically — you didn't have to add them manually. Directus handled that because of the relationship you configured.&lt;/p&gt;
&lt;h3 id=&quot;many-to-many-m2m-products-tags&quot;&gt;Many to Many (M2M): Products ↔ Tags&lt;/h3&gt;
&lt;p&gt;M2M relationships work differently — both sides can have many related records.&lt;/p&gt;
&lt;p&gt;A common real-world example: &lt;strong&gt;tags&lt;/strong&gt; on products, used to improve SEO. A single product can have multiple tags, and a single tag can apply to many products.&lt;/p&gt;
&lt;h4 id=&quot;create-the-tags-collection&quot;&gt;Create the Tags Collection&lt;/h4&gt;
&lt;p&gt;Create a new collection called &lt;strong&gt;tags&lt;/strong&gt;, with the same two fields as before:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Field &lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;name&lt;/code&gt; &lt;/td&gt;&lt;td&gt;String&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;description&lt;/code&gt; &lt;/td&gt;&lt;td&gt;Text (textarea)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Add three or four tags before moving on.&lt;/p&gt;
&lt;h4 id=&quot;wire-up-the-m2m-relationship&quot;&gt;Wire Up the M2M Relationship&lt;/h4&gt;
&lt;p&gt;Back in the &lt;strong&gt;products&lt;/strong&gt; collection:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create Field → Relational category → &lt;strong&gt;Many to Many&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Key: &lt;code&gt;tags&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Related Collection: &lt;code&gt;tags&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;De-select &lt;strong&gt;Enable Create Button&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Do not save yet&lt;/strong&gt; — scroll down and click &lt;strong&gt;&quot;Continue in Advanced Field Creation Mode&quot;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;In the sidebar, click &lt;strong&gt;Relationship&lt;/strong&gt;, then find &lt;strong&gt;Corresponding Field&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Check &lt;strong&gt;&quot;Create Field&quot;&lt;/strong&gt; — under Field Name, you should see &lt;code&gt;products&lt;/code&gt; appear&lt;/li&gt;
&lt;li&gt;Save&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Why the advanced flow?&lt;/strong&gt; Under Corresponding Field, &lt;code&gt;products&lt;/code&gt; appears automatically — that's Directus telling you it's about to create the matching relational field back on the &lt;strong&gt;tags&lt;/strong&gt; collection. Unlike the M2O/O2M setup, you don't have to manually configure both sides — Directus builds the reverse relationship for you.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now, when you add a tag to a product, Directus automatically links that product under the corresponding tag in the &lt;strong&gt;tags&lt;/strong&gt; collection — no manual syncing required.&lt;/p&gt;
&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;Relationship &lt;/th&gt;&lt;th&gt;Direction &lt;/th&gt;&lt;th&gt;Example&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;M2O &lt;/td&gt;&lt;td&gt;Many records → one related record &lt;/td&gt;&lt;td&gt;Products → Branding&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;O2M &lt;/td&gt;&lt;td&gt;One record → many related records &lt;/td&gt;&lt;td&gt;Branding → Products&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;M2M &lt;/td&gt;&lt;td&gt;Many ↔ many, both directions &lt;/td&gt;&lt;td&gt;Products ↔ Tags&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Relationships are one of those Directus features that make a lot more sense once you've built one yourself than they do reading about them — so I'd recommend replicating this setup in your own instance before moving on.&lt;/p&gt;
&lt;p&gt;That wraps up part two. In part three, we'll cover Directus &lt;strong&gt;permissions&lt;/strong&gt; — how to control exactly who can see and edit your data.&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;em&gt;Found this useful? Follow for more Directus and TanStack Start tutorials, or check out the video version on &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.youtube.com/watch?v=7SNrBzaxreg&amp;amp;t=10s&quot;&gt;YouTube&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/24803/directus-basics-part-2-understanding-relationships-m2o-o2m-m2m</guid>
<pubDate>Tue, 18 Aug 2026 00:38:07 +0000</pubDate>
</item>
</channel>
</rss>