Hello!! I have two labels which represent money transfers. We have the column sender, receptor, quantity, date and specific code value whis is different for each transaction:
So I should write:
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:SENDER {sender:c.sender, code:c.code})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:RECEPTOR {receptor:c.receptor, code:c.code})
And then for each of the labels create the constraint:
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:RECEPTOR) ASSERT e.receptor IS UNIQUE
And for making the relation, like this:
MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:transfer]->(b)
But if I have already created the constraint, when I try to merge the rest of the info, I get an error telling me that Node(9546906) already exists with label Sender and property sender = 'Joselu'
My code is:
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:RECEPTOR) ASSERT e.receptor IS UNIQUE
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:SENDER {sender:c.sender, code:c.code})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:RECEPTOR {receptor:c.receptor, code:c.code})
MATCH (n:sender), (b:receptor)
WHERE n.code = b.code
MERGE (n)-[:transfer]->(b)
However, as I have said, when I try the first Load I get the error...
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Sender {sender:c.sender})
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Receptor {receptor:c.receptor})
MATCH (n:Sender), (b:Receptor)
MERGE (n)-[:transfer{code: c.code}]->(b)
Thanks Pilar for all your help, I really appreciate all your effort.
Your code works perfect. It created the unique nodes in the correct way. However, for creating the relationship I need to add the code of the transsaction when I am loading the properties, and it gives me error. I saw you:
code
CREATE CONSTRAINT ON (e:Sender) ASSERT e.sender IS UNIQUE
code
CREATE CONSTRAINT ON (e:Receptor) ASSERT e.receptor IS UNIQUE
3.code
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Sender {sender:c.sender, code:c.code})
code
LOAD CSV WITH HEADERS FROM 'file:///Libro1.csv' AS c FIELDTERMINATOR ';'
Merge (e:Receptor {receptor:c.receptor, code:c.code})
5.code
MATCH (n:Sender), (b:Receptor)
MERGE (n)-[:transfer{code: c.code}]->(b)
In the 1st load with the code:c.code propertie I get: Node(47) already exists with label AA and property emisor = 'Joselu'
Since the constraint is only on sender, It will try to treat only sender as unique. If the sender and code combination is unique, then the constraint should be on the combination.
CREATE CONSTRAINT ON (e:Sender) ASSERT (e.sender, e.code) IS NODE KEY