Search code examples
pythonneo4jneomodel

Python Neo4j Self reference


I am using neomodel library from https://github.com/robinedwards/neomodel . Documentation http://neomodel.readthedocs.org/en/latest/

I have 2 classes Entity and Category - every Category belongs to one Entity, and every Entity can have a parent_entity. For category class this is working:

class Category(StructuredNode):
    name = StringProperty(required=True)
    entity = RelationshipTo(Entity, 'BELONGS_TO', cardinality=One)
    created_at = DateTimeProperty()
    updated_at = DateTimeProperty()

but for Entity class I have written:

class Entity(StructuredNode):
    name = StringProperty(required=True)
    image = StringProperty()
    description = StringProperty()
    parent_entity = Relationship(Entity, 'PARENT', cardinality=ZeroOrMore)
    categories = RelationshipFrom(Category, 'BELONGS_TO', cardinality=ZeroOrMore)
    created_at = DateTimeProperty()
    updated_at = DateTimeProperty()

This is giving me an error that says:

parent_entity = Relationship(Entity, 'PARENT', cardinality=ZeroOrMore)
NameError: name 'Entity' is not defined

How can I implement self referencing model? Any info would be very helpful thanks in advance!


Solution

  • Its because at this point in time the class Entity hasnt been compiled. If you change it to a string 'Entity' it should work as expected.