【问题标题】:AttributeError and ValueError when running py2neo statements in Jupyter notebook在 Jupyter notebook 中运行 py2neo 语句时出现 AttributeError 和 ValueError
【发布时间】:2020-10-31 08:47:20
【问题描述】:

在尝试创建唯一约束或简单合并语句时收到AttributeErrorValueError。然而,约束/节点是在 Neo4j 数据库中创建的。

我正在使用py2neo==2020.0.0

## Create uniqueness constraint (with name)
cypher_str = "CREATE CONSTRAINT UniqueAirportNameConstraint ON (a:Airport) ASSERT a.name IS UNIQUE"
graph.run(cypher_str)

## Merging
params = [{'airline': '3K', 'seats': 500, 'departure': '0915'}]
q = """
    UNWIND $data AS data
    MERGE (s:Schedule)
    """
graph.run(q, { "data": params })

收到错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/miniconda3/envs/neo4j/lib/python3.7/site-packages/py2neo/database/work.py in __init__(self, records, keys)
    889             try:
--> 890                 k = records.keys()
    891             except AttributeError:

AttributeError: 'list' object has no attribute 'keys'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~/miniconda3/envs/neo4j/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/miniconda3/envs/neo4j/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    392                         if cls is not object \
    393                                 and callable(cls.__dict__.get('__repr__')):
--> 394                             return _repr_pprint(obj, self, cycle)
    395 
    396             return _default_pprint(obj, self, cycle)

~/miniconda3/envs/neo4j/lib/python3.7/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    698     """A pprint that just redirects to the normal repr function."""
    699     # Find newlines and replace them with p.break_()
--> 700     output = repr(obj)
    701     lines = output.splitlines()
    702     with p.group():

~/miniconda3/envs/neo4j/lib/python3.7/site-packages/py2neo/database/work.py in __repr__(self)
    367 
    368     def __repr__(self):
--> 369         return repr(self.preview(3))
    370 
    371     def __next__(self):

~/miniconda3/envs/neo4j/lib/python3.7/site-packages/py2neo/database/work.py in preview(self, limit)
    497                 values = self._hydrant.hydrate(keys, values, entities=self._entities, version=v)
    498             records.append(values)
--> 499         return Table(records, keys)
    500 
    501     def evaluate(self, field=0):

~/miniconda3/envs/neo4j/lib/python3.7/site-packages/py2neo/database/work.py in __init__(self, records, keys)
    890                 k = records.keys()
    891             except AttributeError:
--> 892                 raise ValueError("Missing keys")
    893         width = len(k)
    894         t = [set() for _ in range(width)]

ValueError: Missing keys

【问题讨论】:

  • 我会推荐 neo4j 驱动程序而不是 py2neo。 Neo4j 直接支持。

标签: neo4j jupyter-notebook py2neo


【解决方案1】:

根本原因似乎是两个 Cypher 语句都没有返回值,出于某种原因,返回的游标 (py2neo.database.work.Cursor) 期望返回值。

一个简单的解决方法是将输出分配给一个变量,例如:

## Create uniqueness constraint (with name)
cypher_str = "CREATE CONSTRAINT UniqueAirportNameConstraint ON (a:Airport) ASSERT a.name IS UNIQUE"
response = graph.run(cypher_str)

## Merging
params = [{'airline': '3K', 'seats': 500, 'departure': '0915'}]
q = """
    UNWIND $data AS data
    MERGE (s:Schedule)
    """
response = graph.run(q, { "data": params })

注意:response 变量可能可以替换为一次性变量 _

对于MERGE 语句,我们也可以通过添加RETURN 1 或者一些有用的东西来避免这个错误。

【讨论】:

  • 看起来像一个错误。请在 GitHub 存储库中打开一个问题,我会添加一个修复程序。
  • 已添加至link。感谢@NigelSmall 提供py2neo。使用起来相当容易。虽然还没有深入尝试过 OGM。
猜你喜欢
  • 2018-08-13
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 2019-11-21
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多