当我在 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 中一样填充图形。
问题:我怎样才能捕捉到错误,以便其余的导入语句能够正常运行?我 try catch 以下内容但未成功: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())
