cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Create unique node if not exist with unique relation to it

fanka_bacheva
Node Link

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