# Limit M2M items to max 2 per parent via Flows (Directus 11.7.2) – running into skeleton‑row + mis‑count issues

**URL:** <https://community.directus.com/t/limit-m2m-items-to-max-2-per-parent-via-flows-directus-11-7-2-running-into-skeleton-row-mis-count-issues/822>\
**Category:** Help\
**Tags:** data-modeling, flows\
**Created:** [July 24, 2025, 7:48am UTC](https://community.directus.com/t/limit-m2m-items-to-max-2-per-parent-via-flows-directus-11-7-2-running-into-skeleton-row-mis-count-issues/822 "2025-07-24T07:48:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kacper](https://sea2.discourse-cdn.com/flex002/user_avatar/community.directus.com/kacper/32/666_2.png) [@Kacper](https://community.directus.com/u/Kacper)\
**Post date:** [July 24, 2025, 7:48am UTC](https://community.directus.com/t/limit-m2m-items-to-max-2-per-parent-via-flows-directus-11-7-2-running-into-skeleton-row-mis-count-issues/822/1 "2025-07-24T07:48:28Z")

</div>

Hi everyone 👋

I’m stuck on something that feels simple, but I haven’t found a clean solution and would love advice / best‑practice.

### 1 · Context

| Table | Fields (relevant) |
| --- | --- |
| **`pytania`** (`questions`) | `id` (UUID PK), … |
| **`odpowiedzi`** (`answers`) | `id` (UUID PK), … |
| **`pytania_odpowiedzi`** (M2M junction) | `id` (AI PK) · `pytania_id` (FK → questions.id) · `odpowiedzi_id` (FK → answers.id) · `sort` |

Directus 11.7.2, PostgreSQL 14.

**Business rule:** question can have **at most two answers** linked (some sort of quiz).

### 2 · What I tried (Flows only)

- **Trigger** – _Filter (Blocking)_ on `items.create` (collection = `pytania_odpowiedzi`)

- **Read Data** – count rows with  
{ “pytania\_id”: { “\_eq”: “{{$trigger.payload.pytania\_id}}” } }

- **Run Script** – if `existing > 2` → `throw new Error()`.

### 3 · Issues I hit

| Issue | What actually happens |
| --- | --- |
| | |
| **A. skeleton row** | the M2M UI first sends an **INSERT** with only `id` (+ `sort`) – both FKs are `NULL`. My filter fires on that step → if I block it, a record with NULLs is left behind; if I let it pass, the later PATCH inserts the FK values and bypasses my limit. |
| **B. no Min/Max in interface** | I can’t simply tell the field “max items = 2”; that option isn’t available for M2M yet or im blind. |

### 4 · Things I _haven’t_ tried yet

- **Hook** (`extensions/hooks`) – would catch `items.update` after both FKs are present.
- **SQL constraint / trigger** in Postgres – 100 % reliable but moves logic outside Directus.
- **Custom Vue interface** to hide the “Add” button after 2 items (would like to avoid that option).

I’m still hoping to keep it **no‑code (Flows)** if at all possible.

### 5 · Questions to the community

1. Has anyone successfully enforced “max N items” in a junction table _using only Flows_?  
If yes, how did you manage to do that\*
2. Is there a way to make the Read Data filter reliably resolve `{{$trigger.payload.pytania_id}}` (or maybe use `$trigger.keys…`) so it _never_ falls back to a full‑table read?
3. Any news on a built‑in Min/Max setting for relational fields? (I saw feature requests #9445, #19556 but not sure of their status.)

### 6 · Environment

- Directus: **11.7.2** (Docker)
- Database: **PostgreSQL 14**
- Auth: local
- Flows engine enabled, no custom extensions yet.

Any pointers, examples or “don’t bother, use a DB trigger instead” are greatly appreciated!

Thanks 🙏
