OPTIONAL MATCH and ON CREATE doesn't let me create new nodes

I'm trying to merge a new CSV on top of an existing graph, where it merges in new relationships and if the node isn't there yet then it creates one. I tried this but I still get a "source" is missing error.

LOAD CSV WITH HEADERS FROM "file:///crowdfunds_graph.csv" as row
OPTIONAL MATCH (source:Voter {eth: row.source})
OPTIONAL MATCH (target:Voter {eth: row.target})
MERGE (source)-[:crowdfund {contribution: toInteger(row.CF_contribution)}]-(target)
ON CREATE SET source.eth=row.source
ON CREATE SET target.eth=row.target

I'm fairly new to cypher, thanks in advance for any help!

Hi @ath310 ,

I think you are trying to do something like

MERGE (source:Voter {eth: row.source})
MERGE (target:Voter {eth: row.target})
MERGE (source)-[:crowdfund {contribution: toInteger(row.CF_contribution)}]-(target)

If you do an OPTIONAL MATCH at the begining, if there's no MATCH, the MERGE phase will try to create a relationship on a null node. Remember that MERGE applies for the entire selector and not just on fragments.

H