'query with both MATCH and CREATE

I have 3 Nodes:

  • :User which has a unique id property
  • :Palette which has a unique name property
  • :Color which has a unique hex property

When a user saves a pallete I would like to:

  • create a new pallete if a palette with this name does not exist, and add a :CREATED relationship from the :User to the :Palette
  • create a :SAVED relationship from the :User to the :Palette if one does not exist

Afterwards I would like to delete all :INCLUDES relationships that this :Palette has to :Color nodes inside the database (in order to create new ones afterwards).

This is my query (I replaced the variables with hardcoded strings so it's easier to execute):

MATCH (u:User)
    WHERE u.id = '4f3d1904'
MERGE (p:Palette {name: 'Test'})
    ON CREATE 
        SET p.name = "Test"
        MERGE (u)-[cr:CREATED]->(p)
MERGE (u)-[sa:SAVED]->(p)
MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()
DELETE in

When running the query I get the following error:

WITH is required between MERGE and MATCH (line 8, column 1 (offset: 181)) "MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()" ^

But if I add a WITH I get the following:

MATCH (u:User)
    WHERE u.id = '4f3d1904'
MERGE (p:Palette {name: 'Test'})
    ON CREATE 
        SET p.name = "Test"
        MERGE (u)-[cr:CREATED]->(p)
MERGE (u)-[sa:SAVED]->(p)
WITH
MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()
DELETE in

Invalid input ')': expected whitespace or a relationship pattern (line 9, column 32 (offset: 217)) "MATCH (p:Palette {name: 'Test'})-[in:INCLUDES]->()" ^

What am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source