sqlalchemy中对Inherited属性的外键约束

时间:2017-12-02 10:07:15

标签: sqlalchemy

我正在定义一个名为Type1Edge的继承类,它连接到节点,我想确保两个节点在同一系统中,以便不连接到不相关的节点。因此,我想检查两个节点在节点表中是否显示相同的sys_id。我想对继承类Type1Edge执行检查。如果我仅在sys_id中定义BasicEdgeObject我无法检查forigen键约束
ArgumentError: Column(s) 'fss_id' are not part of table 'type_1_edge. 如果我将sys_id放在两者上,我会收到警告 SAWarning: Implicitly combining column basic_edge_object.fss_id with column qualification.fss_id under attribute 'fss_id'. 有没有一种标准的方法呢?。

class BasicEdgeObject(Base):

__tablename__ = 'basic_edge_object'
id = Column(Integer, primary_key=True)
sys_id = Column(Integer, ForeignKey('sys.id'))

__mapper_args__ = {
    'polymorphic_identity': 'basic_edge_object',
    'polymorphic_on': obj_type
}

__mapper_args__ = {
    'polymorphic_identity': 'basic_node_object',
    'polymorphic_on': obj_type
}
class Type1Edge(BasicEdgeObject):

__tablename__ = 'type_1_edge'
id = Column(Integer, ForeignKey('basic_edge_object.id'), primary_key=True)
node_1_id = Column(Integer)
node1= relationship('Node', back_populates='edges', foreign_keys=[node1_id])
node2_id= Column(Integer)
node2= relationship('Node', back_populates='edges',foreign_keys[node2_id])
name = Column(String)

__table_args__ = (ForeignKeyConstraint([node1_id,sys_id],                                         
                  [BasicNodeObject.id,BasicNodeObject.sys_id]),
                  ForeignKeyConstraint([node2_id, sys_id], 
                  [BasicNodeObject.id, BasicNodeObject.sys_id]))

__mapper_args__ = {
    'polymorphic_identity': 'qualification',
}`

0 个答案:

没有答案