当我在py2neo中尝试使用import语句时,会出现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"
最佳答案
您需要正确导入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())