Help with Duplicate Check on Update via Directus Flow

Hello Directus community,

I am trying to implement a duplicate check in a Flow for my cours collection. The Flow works fine for creating new items, but I encounter problems when updating an existing item.

Here is my situation:

  • Collection: cours
  • Fields: Ense (teacher), activite (activity), date_cours (date), heure_debut, heure_fin
  • I want to prevent creating duplicates for the same teacher, activity, and date.

Current setup:

  • A Read Data node to find duplicates
  • An Add Script node to check for duplicates and validate that heure_debut < heure_fin

Problem:
When updating an existing item, the Read Data node throws an error like:

select `cours`.`id_cours` ... where (`cours`.`Ense` = NaN and `cours`.`activite` = NaN ...) - Unknown column 'NaN'

Even when I try to use {{$trigger.key}} in IDs, it still doesn’t work properly. I suspect it is related to referencing the current item vs the payload.

Question:
What is the correct way to implement a duplicate check in Directus Flow for updating items?

  • How should I configure the Read Data node to avoid NaN?
  • How can I filter out the current item being updated from the duplicate check?
  • Is there an example of a working Flow for duplicate validation on update?

Any guidance or working example would be greatly appreciated!
content of node “read data”:
{
“filter”: {
“_and”: [
{
“id_cours”: {
“_neq”: “{{ $trigger.key }}”
}
},
{
“Ense”: {
“_eq”: “{{ $coalesce($trigger.payload.Ense, $trigger.metadata.before.Ense) }}”
}
},
{
“activite”: {
“_eq”: “{{ $coalesce($trigger.payload.activite, $trigger.metadata.before.activite) }}”
}
},
{
“date_cours”: {
“_eq”: “{{ $coalesce($trigger.payload.date_cours, $trigger.metadata.before.date_cours) }}”
}
}
]
}
}
content of node “add script”:
module.exports = async function (data) {
const payload = data.$trigger.payload;
const readata = data.readata || ;

const debut = payload.heure_debut;
const fin = payload.heure_fin;

if (!debut || !fin) {
throw new Error(“Les heures de début et de fin doivent être fournies.”);
}

if (debut >= fin) {
throw new Error(“L’heure de début doit être strictement inférieure à l’heure de fin.”);
}

if (readata.length > 0) {
throw new Error(“Erreur : ce cours existe déjà pour cette date.”);
}

return payload;
};


Thank you in advance!