Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How do I cast optional nodes? #132

Open
g3rd opened this issue Dec 13, 2016 · 2 comments
Open

Question: How do I cast optional nodes? #132

g3rd opened this issue Dec 13, 2016 · 2 comments

Comments

@g3rd
Copy link

g3rd commented Dec 13, 2016

Query:

MATCH (person:Person {id: 1})
OPTIONAL MATCH (person)-[:OWNS]->(product:Product)
RETURN person, product

Execute Query

with graph.transaction(commit=False) as tx:
    results = graph.query(q, returns=(client.Node, client.Node, ))

Exception

If I execute the Query and it has the optional match, no exception. If the optional match returns None I get:

  File "/usr/local/lib/python3.5/site-packages/neo4jrestclient/query.py", line 829, in __exit__
    self.commit()
  File "/usr/local/lib/python3.5/site-packages/neo4jrestclient/query.py", line 948, in commit
    results = self._execute(url, results=True)
  File "/usr/local/lib/python3.5/site-packages/neo4jrestclient/query.py", line 868, in _execute
    _results = self._update(content["results"])
  File "/usr/local/lib/python3.5/site-packages/neo4jrestclient/query.py", line 891, in _update
    obj, elements=result["data"], returns=returns
  File "/usr/local/lib/python3.5/site-packages/neo4jrestclient/query.py", line 792, in cast
    casted_row.append(cast_element(element, func))
  File "/usr/local/lib/python3.5/site-packages/neo4jrestclient/query.py", line 714, in cast_element
    obj = func(element["self"], update_dict=element,
TypeError: 'NoneType' object is not subscriptable
@g3rd
Copy link
Author

g3rd commented Dec 13, 2016

Updated the Execute Query section to include the transaction.

@versae
Copy link
Owner

versae commented Dec 16, 2016

It looks like I should check whether element is None before getting element["self"]. In the meantime you could try casting to neo4jrestclient.constants.RAW:

with graph.transaction(commit=False) as tx:
    results = graph.query(q, returns=(RAW, client.Node, ))

Or even create your own casting function:

with graph.transaction(commit=False) as tx:
    results = graph.query(q,
        returns=(lambda x: graph.node(x["self"]) if x and "self" in x else None,
                 client.Node, ))

Let me know if any of this approaches work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants