Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 850741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:18:49+00:00 2026-05-15T07:18:49+00:00

Related (for the no-association-object use case): SQLAlchemy Many-to-Many Relationship on a Single Table Building

  • 0

Related (for the no-association-object use case): SQLAlchemy Many-to-Many Relationship on a Single Table

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:18:49+00:00Added an answer on May 15, 2026 at 7:18 am

    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:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got to Tables related in one-to-many association: Product*1 - n*Inventory @Entity public class
Suppose i have two tables with one to many relationship (association in EF 4.1)
Related to: Query Windows Search from Java But this time to use OSX's spotlight
I have two models Company and Role related by has_many and belongs_to association respectively.
Is there a way to map a many-to-many relationship with properties on each item
I have 2 tables (say parent and child table and 1 to many relation),
If I have a polymorphic association, how do I access related methods in my
I have some entity in my application,and some of them are many-to-many association,when I
I have two related tables, MANY and ONE. An integer ONE_ID column on the
I have the following issue related to iterating over an associative array of strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.