Create unique node if not exist with unique relation to it

Hello :slight_smile:
I have the following nodes:

  • Catalog{id, name}
  • User{id, email}

I want to:

  • create Catalog node if there is no such one
  • add relationship [CREATED_BY{createdOn:datetime()}]

MERGE (c:Collection{name:"Holiday meals"})
WITH c
MATCH (u:User{email:"test@gmai.com"})
MERGE (u)-[rel:COLLECTION_CREATED{createdOn:datetime()}]->(c)
RETURN c

Unfortunately there is no uniqueness because of createdOn property in the relationship and every time there is a new relation created between the nodes.

What can I do to fix this?
Any suggestions about changes in architecture or current implementations will be welcomed :slight_smile:
Thanks

You need to move setting the onCreated property into a ON CREATE handler:

WITH c
MATCH (u:User{email:"[test@gmai.com](mailto:test@gmai.com)"})
MERGE (u)-[rel:COLLECTION_CREATED]->(c)
ON CREATE SET rel.createdOn = datetime()
RETURN c```
1 Like