cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create unique node if not exist with unique relation to it

Hello
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
Thanks

1 ACCEPTED SOLUTION

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```

View solution in original post

1 REPLY 1

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```