Python Neo4j自我参考

时间:2015-02-01 20:59:53

标签: python neo4j neomodel

我正在使用来自https://github.com/robinedwards/neomodel的neomodel库。文档http://neomodel.readthedocs.org/en/latest/

我有2个类实体和类别 - 每个类别属于一个实体,每个实体都可以有一个parent_entity。对于类别类,这是有效的:

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

但对于Entity类我写过:

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()

这给了我一个错误说:

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

如何实现自引用模型?提前感谢任何信息都会非常有用!

1 个答案:

答案 0 :(得分:3)

因为此时类Entity尚未编译。如果您将其更改为字符串'Entity',它应该按预期工作。