简体   繁体   English

与关联对象在同一个表上的多对多关系

[英]Many-to-many relationship on same table with association object

Related (for the no-association-object use case): SQLAlchemy Many-to-Many Relationship on a Single Table 相关(对于无关联对象用例): SQLAlchemy单表上的多对多关系

Building a many-to-many relationship is easy. 建立多对多关系很容易。 Building a many-to-many relationship on the same table is almost as easy, as documented in the above question. 如上面的问题所述,在同一个表上建立多对多关系几乎一样容易。

Building a many-to-many relationship with an association object is also easy. 与关联对象建立多对多关系也很容易。

What I can't seem to find is the right way to combine association objects and many-to-many relationships with the left and right sides being the same table. 我似乎无法找到的是将关联对象和多对多关系组合在一起的正确方法,左右两侧是同一个表。

So, starting from the simple, naïve, and clearly wrong version that I've spent forever trying to massage into the right version: 所以,从简单,天真,明显错误的版本开始,我花了很长时间试图按摩到正确的版本:

t_groups = Table('groups', metadata,
    Column('id', Integer, primary_key=True),
)

t_group_groups = Table('group_groups', metadata,
    Column('parent_group_id', Integer, ForeignKey('groups.id'), primary_key=True, nullable=False),
    Column('child_group_id', Integer, ForeignKey('groups.id'), primary_key=True, nullable=False),
    Column('expires', DateTime),
)

mapper(Group_To_Group, t_group_groups, properties={
    'parent_group':relationship(Group),
    'child_group':relationship(Group),
})

What's the right way to map this relationship? 映射这种关系的正确方法是什么?

I guess you are getting an error like Could not determine join condition between parent/child tables... In this case, Change the mapper for Group_To_Group to the following: 我猜你得到的错误就像Could not determine join condition between parent/child tables...在这种情况下,将Group_To_Group的映射器更改为以下内容:

mapper(Group_To_Group, t_group_groups, properties={
    'parent_group':relationship(Group,
        primaryjoin=(t_group_groups.c.parent_group_id==t_groups.c.id),),
    'child_group':relationship(Group,
        primaryjoin=(t_group_groups.c.child_group_id==t_groups.c.id),),
})

also you might want to add the backref so that you can navigate the relations from the Group objects as well. 您也可以添加backref以便也可以从Group对象中导航关系。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多对多关联表上的SQLAlchemy关系 - SQLAlchemy relationship on many-to-many association table SQLAlchemy使用关联表以多对多关系插入数据 - SQLAlchemy Inserting Data in a Many-to-Many Relationship with Association Table 自引用多对多关系与 SQLAlchemy 中的关联 object - Self-referencing many-to-many relationship with an association object in SQLAlchemy 自引用多对多关系与关联 object 中的额外列 - Self referencing many-to-many relationship with extra column in association object Flask-AppBuilder 同一个表中的多对多关系 - Flask-AppBuilder Many-to-Many relationship within the same Table 如何在SQLAlchemy中与关联对象(没有关联代理)建立多对多关系? - How can I access extra columns in SQLAlchemy a many-to-many relationship with an association object (without an association proxy)? 插入 flask SQLAlchemy 与关联表以多对多关系插入数据时出错 - Error while inseting flask SQLAlchemy Inserting Data in a Many-to-Many Relationship with Association Table SQLAlchemy 1.4 与关联表的多对多关系重叠关系的警告 - SQLAlchemy 1.4 warnings on overlapping relationships with a many-to-many relationship with association table 通过SqlAlchemy中的关联对象实现多对多,自引用,非对称关系(推特模型) - Many-to-many, self-referential, non-symmetrical relationship (twitter model) via Association Object in SqlAlchemy 多对多表之间的关系 - Relationship across a many-to-many table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM