Py2neo -- how to fix circular references?


(Brendan Foley) #1

We are trying to build a full OGM for a Python/Flask based backend which uses Neo4j.

The problem I am running into is with the RelatedTo and RelatedFrom parts of Py2neo.ogm framework.

Naturally, if my graph has a structure like (a)-[:LINKS_TO]->(b) I would want OGM properties:
class a(GraphObject):
links_to = RelatedTo(b, "HAS")
class b(GraphObject):
linked_to = RelatedFrom(a, "HAS")

Note: I have used the objects a and b here not 'a' and 'b'. When I switch to using quotes it does not seem to retrieve the GraphObjects at all.

This causes Python to freak out with the circular dependencies since a is referencing b before it is defined.

I have not found a way to "forward declare" classes in Python like you can in some other languages.

This must be an extremely common situation in Py2neo, how are others solving it?