와 다형성 상속 나는 클래스 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'
에 의지 해) 불평하고 :
이유 내가 나중에 소개 할 수 Leaf (Node)
의 모든 하위 클래스에 대한 정의와 Node
을 연장하지 않기 때문에 내가 정의입니다 이동하고자합니다. 더구나 나는 Leaf.root
와 Leaf.base
(Node
에 의해 제공됨)을 가지고 있으므로 _x
과 _y
이 필요하지 않습니다. 하지만 는에게이를 생략 등의 문제에 대해 호출 (_x =
& _y =
) :
AttributeError: 'Node' object has no attribute 'leafs'
나는 어떤을 사용하지 않아도에도 불구하고, 나는에 관계를 연결 Leaf (Node)
에 뭔가를 사용하는 데 필요한 것 같아 내 원래 정의에있는 leafs
및 subleafs
의 배경 정보는 Node
입니다. 고마워.