py2neo引发ConstraintViolation错误,shell没有

时间:2015-06-05 13:04:46

标签: python neo4j py2neo

当我在py2neo中尝试导入语句时出现ConstraintViolation错误,但是当我直接在neo4j的shell中导入时,同样的错误永远不会出现。我在两者中都使用了相同的语句,在py2neo中我只使用graph.cypher.execute(...)。请注意,我有多次确保每个ID都是唯一的 - 没有重复的值。

py2neo.cypher.error.schema.ConstraintViolation: Node 0 already exists with label Employee and property "ID"=[XXXX]

使用py2neo,即使调用了错误并且它结束了程序,整个命令仍然耗尽,就像在neo4j shell中一样填充图形。

问题:如何捕获错误,以便我的其他导入语句可以正常运行?我尝试捕获以下内容但未成功:error.ConstraintViolation,error.schema.ConstraintViolation。

此外:点击该错误后继续导入,这很好。然而,进口仍在继续"继续"打印。

try:
    graph.cypher.execute(statement)
except ConstraintViolation as e:
    # print(traceback.format_exec())
     print "ConstraintViolation error"
print "continues"

1 个答案:

答案 0 :(得分:4)

您需要正确导入ConstraintViolation并捕获它:

from py2neo.cypher.error.schema import ConstraintViolation
import traceback

try:
    # your cypher.execute() here

except ConstraintViolation as e:
    # do whatever you want to do when the error occurs
    # e.g. print the traceback of the error
    print(traceback.format_exc())