Closed
Description
Hi,
I'm using a MERGE query to create/update a relationship using beginTransaction() .... commit() model. Though it works fine, run() is giving me the correct relationShipsCreated/propertiesSet values, commit() is not giving the same result. I expect the commit() function to give the same result as well as it is the one which is actually "committing". I posted some sample code and output below:
function queryDB(query, params) {
return new Promise(function (resolve, reject) {
var session = db.session()
session.run(query, params)
.then(function (result) {
session.close();
resolve(result)
})
.catch(function (error) {
console.log(error);
reject(error)
});
});
}
async function createRelation() {
var query = "MATCH (m), (n) WHERE m.name = 'Joe' AND n.name = 'Mike' CREATE (m)-[r: TEACHES {courses: 2}]->(n)"
try {
var result = await queryDB(query);
console.log("Created relation: " + JSON.stringify(result));
} catch (ex) {
console.log(ex)
throw ex
}
}
async function createUpdateRelation() {
var query = "MATCH (m:TEACHER {name: 'Joe'}) " +
"MATCH (n:STUDENT {name: 'Mike'}) " +
"WITH m, n " +
"MERGE (m)-[r: TEACHES]->(n) " +
" ON CREATE SET r.courses = 2 " +
" ON MATCH SET r.courses = 3 " +
"RETURN m.name as teacher, r.courses as courses, n.name as student"
const session = db.session();
var tx = session.beginTransaction();
var queries = []
try {
var result = await tx.run(query);
console.log("\nQuery output: " + JSON.stringify(result));
var commitResult = await tx.commit()
console.log("Commit completed: " + JSON.stringify(commitResult));
} catch(ex) {
console.log("Exception: " + ex)
console.log("Rolling back transaction");
tx.rollback();
}
session.close();
}
async function test() {
try {
//await createNodes();
//await createRelation();
await createUpdateRelation();
console.log("Done");
} catch(ex) {
console.log(ex);
}
}
test();
/*
Output:
Created teacher: {"records":[{"keys":["n"],"length":1,"_fields":[{"identity":{"low":40,"high":0},"labels":["TEACHER"],"properties":{"name":"Joe"}}],"_fieldLookup":{"n":0}}],"summary":{"statement":{"text":"CREATE (n:TEACHER {name: 'Joe'}) RETURN n","parameters":{}},"statementType":"rw","counters":{"_stats":{"nodesCreated":1,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"propertiesSet":1,"labelsAdded":1,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"updateStatistics":{"_stats":{"nodesCreated":1,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"propertiesSet":1,"labelsAdded":1,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"plan":false,"profile":false,"notifications":[],"server":{"address":"192.168.100.1:7687","version":"Neo4j/3.2.1"},"resultConsumedAfter":{"low":0,"high":0},"resultAvailableAfter":{"low":1,"high":0}}}
Created student: {"records":[{"keys":["n"],"length":1,"_fields":[{"identity":{"low":41,"high":0},"labels":["STUDENT"],"properties":{"name":"Mike"}}],"_fieldLookup":{"n":0}}],"summary":{"statement":{"text":"CREATE (n:STUDENT {name: 'Mike'}) RETURN n","parameters":{}},"statementType":"rw","counters":{"_stats":{"nodesCreated":1,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"propertiesSet":1,"labelsAdded":1,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"updateStatistics":{"_stats":{"nodesCreated":1,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"propertiesSet":1,"labelsAdded":1,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"plan":false,"profile":false,"notifications":[],"server":{"address":"192.168.100.1:7687","version":"Neo4j/3.2.1"},"resultConsumedAfter":{"low":0,"high":0},"resultAvailableAfter":{"low":1,"high":0}}}
Create new relationship:
Query output: {"records":[{"keys":["teacher","courses","student"],"length":3,"_fields":["Joe",{"low":2,"high":0},"Mike"],"_fieldLookup":{"teacher":0,"courses":1,"student":2}}],"summary":{"statement":{"text":"MATCH (m:TEACHER {name: 'Joe'}) MATCH (n:STUDENT {name: 'Mike'}) WITH m, n MERGE (m)-[r: TEACHES]->(n) ON CREATE SET r.courses = 2 ON MATCH SET r.courses = 3 RETURN m.name as teacher, r.courses as courses, n.name as student","parameters":{}},"statementType":"rw","counters":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"relationshipsCreated":1,"relationshipsDeleted":0,"propertiesSet":1,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"updateStatistics":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"**relationshipsCreated**":1,"relationshipsDeleted":0,"**propertiesSet**":1,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"plan":false,"profile":false,"notifications":[],"server":{"address":"192.168.100.1:7687","version":"Neo4j/3.2.1"},"resultConsumedAfter":{"low":0,"high":0}}}
Commit completed: {"records":[],"summary":{"statement":{"text":"COMMIT","parameters":{}},"counters":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"propertiesSet":0,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"updateStatistics":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"**relationshipsCreated**":0,"relationshipsDeleted":0,"**propertiesSet**":0,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"plan":false,"profile":false,"notifications":[],"server":{}}}
Done
Update existing relationship:
Query output: {"records":[{"keys":["teacher","courses","student"],"length":3,"_fields":["Joe",{"low":3,"high":0},"Mike"],"_fieldLookup":{"teacher":0,"courses":1,"student":2}}],"summary":{"statement":{"text":"MATCH (m:TEACHER {name: 'Joe'}) MATCH (n:STUDENT {name: 'Mike'}) WITH m, n MERGE (m)-[r: TEACHES]->(n) ON CREATE SET r.courses = 2 ON MATCH SET r.courses = 3 RETURN m.name as teacher, r.courses as courses, n.name as student","parameters":{}},"statementType":"rw","counters":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"propertiesSet":1,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"updateStatistics":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"**propertiesSet**":1,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"plan":false,"profile":false,"notifications":[],"server":{"address":"192.168.100.1:7687","version":"Neo4j/3.2.1"},"resultConsumedAfter":{"low":0,"high":0}}}
Commit completed: {"records":[],"summary":{"statement":{"text":"COMMIT","parameters":{}},"counters":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"propertiesSet":0,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"updateStatistics":{"_stats":{"nodesCreated":0,"nodesDeleted":0,"relationshipsCreated":0,"relationshipsDeleted":0,"**propertiesSet**":0,"labelsAdded":0,"labelsRemoved":0,"indexesAdded":0,"indexesRemoved":0,"constraintsAdded":0,"constraintsRemoved":0}},"plan":false,"profile":false,"notifications":[],"server":{}}}
Done
*/
In the above code, in:
- "Create new relationship: " Ideally in "Query output: ... "relationshipsCreated":1, "propertiesSet":1 .."
should match with "Commit completed: ... "relationshipsCreated":0, "propertiesSet":0 ..." - Same issues in "Update existing relationship:" propertiesSet does not match with the run() output and commit() output.
Metadata
Metadata
Assignees
Labels
No labels