Node Exists Error on Property Set


Still new at this, but hopefully getting better.
I have a data file with three columns
level, sort_key, parent where sort_key and parent are the same types

I load with this and it works fine
LOAD CSV WITH HEADERS FROM 'file:///cpc-scheme-A.csv' AS row
MERGE (p:classification{class_key:row.sort_key})
MERGE (q:classification{class_key:row.parent})

Next I add relationships with this and it works fine:
LOAD CSV WITH HEADERS FROM 'file:///cpc-scheme-A.csv' AS row
MATCH (p:classification{class_key:row.sort_key})
MATCH (o:classification{class_key:row.parent})
MERGE (p)-[:child_of]->(o)

Lastly I want to set a property key and am trying this:
LOAD CSV WITH HEADERS FROM 'file:///cpc-scheme-A.csv' AS row
MATCH (p:classification{class_key:row.sort_key})
SET p.level =row.level

this returns an error
Node(0) already exists with label classification and property level = '5'

If I run this
Match (p:classification{level:'5'})
Return id(p),p.class_key,p.level

it returns no records (no changes, no records)

Where am I amiss?

Looks like the .csv file headers have blank spaces and that's why no nodes were created. You need to enclose the header name in back tics.

Try running this to see the blank spaces in headers:

LOAD CSV WITH HEADERS FROM 'file:///cpc-scheme-A.csv' AS row
return row limit 2 and see the empty spaces. 

Also you can create nodes and relationship in one run.

MERGE (p:classification{class_key: row.` sort_key`,level:row.level })
MERGE (q:classification{class_key: row.` parent `})
MERGE (p)-[:child_of]->(q)


I guess I wasn't clear.
Step 1. create nodes works as expected :white_check_mark:
Step 2. create relationships works as expected :white_check_mark:
Step 3 Add property values to node does not :x:

This is the statement I am using to set the property values
LOAD CSV WITH HEADERS FROM 'file:///cpc-scheme-A.csv' AS row
MATCH (p:classification{class_key:row.sort_key})
SET p.level =row.level

Note: I had previously used the first two lines successfully when setting relationships.
This returns an error.

That type of message suggests you have a unique constraint on :classification(level).

In that case, there are two scenarios that could be happening here, both resulting from the processing of the data during your query (this is why your match is returning no results, because the tx that applied the results was rolled back).

First scenario is that the CSV has level 5 for multiple rows/nodes (and likely similar for other levels), and this would indicate that the unique constraint on :classification(level) is probably a mistake, and you might want either a node key constraint or just an index instead.

Next scenario, assuming the constraint is what you actually want, is that you have multiple nodes with the same class_key value. That would mean during processing that your MATCH finds multiple nodes for the same class_key (and level 5 in the CSV), and tries to set all of their levels to 5, conflicting with your unique constraint.

Hi Andrew,

I had set a constraint earlier and removing it did fix the issue.
Thank you