Inheritance SQLAlchemy和带backrefs的多态性继承

Inheritance SQLAlchemy和带backrefs的多态性继承,inheritance,sqlalchemy,flask-sqlalchemy,polymorphism,backreference,Inheritance,Sqlalchemy,Flask Sqlalchemy,Polymorphism,Backreference,我有如下所示的类Node和Leaf(Node):它工作正常,但我更愿意将leafs和subbafs定义转移到Leaf(Node)类。我如何做到这一点 class Node (db.Model): __mapper_args__ = {'polymorphic_identity':'node', 'polymorphic_on':'type'} id = db.Column (db.Integer, primary_key=True) type = db.Column ('

我有如下所示的类
Node
Leaf(Node)
:它工作正常,但我更愿意将
leafs
subbafs
定义转移到
Leaf(Node)
类。我如何做到这一点

class Node (db.Model):
    __mapper_args__ = {'polymorphic_identity':'node', 'polymorphic_on':'type'}
    id = db.Column (db.Integer, primary_key=True)
    type = db.Column ('type', db.String (16))

    root_id = db.Column (db.Integer, db.ForeignKey (id))
    nodes = db.relationship ('Node',
        cascade='all', lazy='dynamic',
        primaryjoin='Node.root_id==Node.id',
        backref=db.backref('root', remote_side=id))
    leafs = db.relationship ('Leaf',
        cascade='all', lazy='dynamic',
        primaryjoin='Leaf.root_id==Node.id')

    base_id = db.Column (db.Integer, db.ForeignKey (id))
    subnodes = db.relationship ('Node',
        cascade='all', lazy='dynamic',
        primaryjoin='Node.base_id==Node.id',
        backref=db.backref('base', remote_side=id))
    subleafs = db.relationship ('Leaf',
        cascade='all', lazy='dynamic',
        primaryjoin='Leaf.base_id==Node.id')

    def __init__ (self, root):
        self.base = root.base if root and root.base else root
        self.root = root

我尝试了这个,但失败了(部分):

我的delete测试用例不喜欢这样(只是删除树中的基本/根节点并依赖
cascade='all'
),并抱怨:

CircularDependencyError: Circular dependency detected. Cycles: set([DeleteState(<Leaf at 0x22789d0>)]) all edges: set([(DeleteState(<Leaf at 0x22789d0>), DeleteState(<Leaf at 0x22789d0>))])

我想我需要使用
Leaf(Node)
中的某些内容来附加关系,即使我在
Node
中的
leafs
subleafs
的原始定义中不需要使用任何backref。Thx.

好吧,在尝试了一些之后,我找到了一个解决方案,这不是完美的,但是完成了工作:我只是将
节点。leafs
节点。subbafs
定义移动到文件
leaf.py
中,其中定义了
类叶(节点)
,并附加了前面的定义,如下所示:

class Leaf (Node):
    __mapper_args__ = {'polymorphic_identity': 'leaf'}
    leaf_id = db.Column (db.Integer, db.ForeignKey ('node.id'), primary_key=True)

    def __init__ (self, root):
        super (Leaf, self).__init__ (root)

Node.leafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
    primaryjoin=Node.id==Leaf.root_id)

Node.subleafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
    primaryjoin=Node.id==Leaf.base_id)
这样做的一个缺点是,如果人们想访问
节点.leafs
节点.subleafs
,就必须从模型导入Leaf,但由于他们无论如何都必须这样做(即使
节点.leafs
节点.subleafs
类叶(节点)
中被定义为backrefs),没关系


如果有人找到了一个解决方案,其中关系被定义为
类叶(节点)
中的backrefs,我很高兴听到您的消息;thx.

好吧,在尝试了一些之后,我找到了一个解决方案,这不是完美的,但是完成了工作:我只是将
节点。leafs
节点。subbafs
定义移动到文件
leaf.py
中,其中定义了
类叶(节点)
,并附加了前面的定义,如下所示:

class Leaf (Node):
    __mapper_args__ = {'polymorphic_identity': 'leaf'}
    leaf_id = db.Column (db.Integer, db.ForeignKey ('node.id'), primary_key=True)

    def __init__ (self, root):
        super (Leaf, self).__init__ (root)

Node.leafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
    primaryjoin=Node.id==Leaf.root_id)

Node.subleafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
    primaryjoin=Node.id==Leaf.base_id)
这样做的一个缺点是,如果人们想访问
节点.leafs
节点.subleafs
,就必须从模型导入Leaf,但由于他们无论如何都必须这样做(即使
节点.leafs
节点.subleafs
类叶(节点)
中被定义为backrefs),没关系

如果有人找到了一个解决方案,其中关系被定义为
类叶(节点)
中的backrefs,我很高兴听到您的消息;thx

AttributeError: 'Node' object has no attribute 'leafs'
class Leaf (Node):
    __mapper_args__ = {'polymorphic_identity': 'leaf'}
    leaf_id = db.Column (db.Integer, db.ForeignKey ('node.id'), primary_key=True)

    def __init__ (self, root):
        super (Leaf, self).__init__ (root)

Node.leafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
    primaryjoin=Node.id==Leaf.root_id)

Node.subleafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
    primaryjoin=Node.id==Leaf.base_id)