Python 多态类子集的Sqlalchemy外键

Python 多态类子集的Sqlalchemy外键,python,database-design,orm,sqlalchemy,polymorphic-associations,Python,Database Design,Orm,Sqlalchemy,Polymorphic Associations,我正在使用sqlalchemy创建一个类似于图形的结构,以便有几种类型的节点和链接连接它们。节点的定义如下所示: class Node(Base): __tablename__ = 'node' id = Column(Integer, primary_key=True) type = Column(Unicode) __mapper_args__ = {'polymorphic_on': type} class InputNode(Node): __t

我正在使用sqlalchemy创建一个类似于图形的结构,以便有几种类型的节点和链接连接它们。节点的定义如下所示:

class Node(Base):
    __tablename__ = 'node'
    id = Column(Integer, primary_key=True)
    type = Column(Unicode)
    __mapper_args__ = {'polymorphic_on': type}

class InputNode(Node):
    __tablename__ = 'inputnode'
    id = Column(Integer, ForeignKey('node.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'input'}

class ThruNode(Node):
    __tablename__ = 'thrunode'
    id = Column(Integer, ForeignKey('node.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'thru'}

class OutputNode(Node):
    __tablename__ = 'outputnode'
    id = Column(Integer, ForeignKey('node.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'output'}
class Link(Base):
    __tablename__ = 'link'
    input = Column(Integer, ForeignKey('node.id', where='type IN ("input", "thru")'))
    output = Column(Integer, ForeignKey('node.id', where='type IN ("thru", "output")'))
现在我想创建一个
链接
表,该表如下所示:

class Node(Base):
    __tablename__ = 'node'
    id = Column(Integer, primary_key=True)
    type = Column(Unicode)
    __mapper_args__ = {'polymorphic_on': type}

class InputNode(Node):
    __tablename__ = 'inputnode'
    id = Column(Integer, ForeignKey('node.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'input'}

class ThruNode(Node):
    __tablename__ = 'thrunode'
    id = Column(Integer, ForeignKey('node.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'thru'}

class OutputNode(Node):
    __tablename__ = 'outputnode'
    id = Column(Integer, ForeignKey('node.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'output'}
class Link(Base):
    __tablename__ = 'link'
    input = Column(Integer, ForeignKey('node.id', where='type IN ("input", "thru")'))
    output = Column(Integer, ForeignKey('node.id', where='type IN ("thru", "output")'))

我正在努力解决的问题是如何做
where
部分,因为正如我所写的,它在sqlalchemy中是无效的。我曾想过使用
检查约束
外来键约束
,但我看不出它们中的任何一个可以实际用于此操作。

我没有尝试过,也不是这方面的专家,但这不应该奏效吗

class Link(Base):
__tablename__ = 'link'
input = Column(Integer, ForeignKey('thrunode.id'))
output = Column(Integer, ForeignKey('outputnode.id'))
首先,我有另一个想法,也许你可以为ID使用不同的名称,而不是使用这些名称,比如:
而不是:

类输入节点(节点):
__tablename\uuu='inputnode'
id=列(整数,ForeignKey('node.id'),primary_key=True)
__mapper_args_uu={'polymorphic_identity':'input'}

这:

类通过节点(节点):
[...]
thrunode_id=Column(整数,ForeignKey('node.id'),primary_key=True)
[…]

然后:

class Link(Base):
__tablename__ = 'link'
input = Column(Integer, ForeignKey('node.thrunode_id'))
output = Column(Integer, ForeignKey('node.outputnode_id'))

我是从这里得到这个想法的

有什么更新吗?@jpmorin没有。幸运的是,这个项目现在已经被搁置,所以我不需要担心。然而,我认为真正的解决方案应该是使用像neo4j这样的nosql数据库。我仍然想知道如何在sqlalchemy中实现它。这将输入限制到ThruNode,这不是我想要的。我想说的是,它可以是一个ThruNode,也可以是一个InputNode,我明白了。我没有意识到这一点。另一种可能是让thru节点从节点继承,然后让输入节点和输出节点从thrunode继承。但也许我应该停止回答我不知道的问题。。。