1

와 다형성 상속 나는 클래스 Node 아래와 같이 Leaf (Node) 있습니다. 어떻게해야합니까? SQLAlchemy의와 역 참조

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) 

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 

나는 (부분적으로)이 시도했지만 실패

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

    _x = db.relationship ('Node', backref=db.backref ('leafs', 
     cascade='all', lazy='dynamic', primaryjoin='Leaf.root_id==Node.id')) 
    _y = db.relationship ('Node', backref=db.backref ('subleafs', 
     cascade='all', lazy='dynamic', primaryjoin='Leaf.base_id==Node.id')) 

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

테스트 케이스 그냥 기본/루트 노드를 삭제 (이 좋아하지 않았다 삭제 나무에서 그리고 cascade='all'에 의지 해) 불평하고 :

,466,155,623,783,497,603,210

이유 내가 나중에 소개 할 수 Leaf (Node)의 모든 하위 클래스에 대한 정의와 Node을 연장하지 않기 때문에 내가 정의입니다 이동하고자합니다. 더구나 나는 Leaf.rootLeaf.base (Node에 의해 제공됨)을 가지고 있으므로 _x_y이 필요하지 않습니다. 하지만 에게이를 생략 등의 문제에 대해 호출 (_x = & _y =) :

AttributeError: 'Node' object has no attribute 'leafs' 

나는 어떤을 사용하지 않아도에도 불구하고, 나는에 관계를 연결 Leaf (Node)뭔가를 사용하는 데 필요한 것 같아 내 원래 정의에있는 leafssubleafs의 배경 정보는 Node입니다. 고마워.

답변

1

글쎄, 일부는 내가 해결책을 발견했습니다 시도한 후, 그 완벽하지 않지만 일을 : 난 그냥 class Leaf (node)이 전 정의 등으로 정의가 추가 된 파일 leaf.pyNode.leafsNode.subleafs 정의를 이동 아래 :

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) 

이의 단점은 사람들이 Node.leafsNode.subleafs에 액세스하려면 from models import Leaf을 가지고 있지만이 어쨌든해야했다 있기 때문에 (경우에도 Node.leafsNode.subleafs이내에서 역 참조로 정의되었던 것입니다), 괜찮습니다.

누군가가 솔루션을 찾은 경우, 관계가 class Leaf (Node) 내의 백 파일로 정의되는 곳에서 정보를 얻을 수 있습니다. 고마워.