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 catchitems.updateafter 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
- Has anyone successfully enforced “max N items” in a junction table using only Flows?
If yes, how did you manage to do that* - 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? - 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 ![]()