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 6390919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:36:27+00:00 2026-05-25T03:36:27+00:00

I am having problems configuring a many to many relationship to a model itself.

  • 0

I am having problems configuring a many to many relationship to a model itself. I can configure a self-many-to-many relation when I use anormal relationship configuration i.e. one not using an Association object.

In this scenario I have to record some extra information in the many-to-many table itself so I’m trying to implement relationship using an Association object (PageLink).

Here are the models.

class PageLink(Base):
    '''
    Association table.
    '''
    __tablename__ = 'page_links'

    id = Column(Integer,primary_key=True)
    page_from = Column(Integer,ForeignKey('page.id'),primary_key=True)
    page_to = Column(Integer,ForeignKey('page.id'),primary_key=True)
    extra_col1 = Column(String(256),nullable=False)

class Page(Base):
    '''
    main table
    '''

    __tablename__ = 'page'

    id = Column(Integer,primary_key=True)
    name = Column(String(56),nullable=False)

    linked = relationship('PageLinks',backref='parent_page',
                          primaryjoin=id==PageLink.page_from,
                          secondaryjoin=id==PageLink.page_to)

This approach does not work. I have tried removing ‘secondaryjoin’ keyword but it wouldn’t work.

Would greatly appreciate any help or advice on this matter.

Thank you for reading.

  • 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-25T03:36:28+00:00Added an answer on May 25, 2026 at 3:36 am

    The association object pattern is not a sepecialization of the many-to-many relationship, but rather a special case of one-to-many relationships where you have a left_table – Many-To-One – association_table – One-To-Many – right_table set up. In short, you need two relationships, neither of which should have a secondary/secondaryjoin.

    class PageLink(Base):
        '''
        Association table.
        '''
        __tablename__ = 'page_links'
    
        id = Column(Integer,primary_key=True)
        page_from = Column(Integer,ForeignKey('page.id'),primary_key=True)
        page_to = Column(Integer,ForeignKey('page.id'),primary_key=True)
        extra_col1 = Column(String(256),nullable=False)
    
    class Page(Base):
        '''
        main table
        '''
    
        __tablename__ = 'page'
    
        id = Column(Integer,primary_key=True)
        name = Column(String(56),nullable=False)
    
        linked_to = relationship('PageLinks',backref='parent_page',
                                 primaryjoin=id==PageLink.page_from)
        linked_from = relationship('PageLinks',backref='child_page',
                                   primaryjoin=id==PageLink.page_to)
    

    which means, to access the extra column for the ‘to’ links from some page p, you have to do: p.linked_to[0].extra_col1, or to get the actual linked page, p.linked_to[0].page_to


    As an aside, it’s often a great idea to use either an autoincrement primary key or (left/right) foreign key pair as the primary key in associations, but almost never useful to have both in the primary key. An alternative that combines both ideas would be to use an autoincrement integer as the only column in the primary key, and have an additional unique constraint on the left/right foreign key columns.

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

Sidebar

Related Questions

I am having problems configuring JRuby to work properly with OpenSSL. Googling has revealed
am having problems with some Regex code can anyone help. I have the following
This is my first time using nginx and I'm having some problems configuring an
I'm just starting using the Log4Net library and having problems configuring it. I don't
I am having problems using a LoginView for what I need. Can somebody tell
I am having ongoing problems configuring my Spring Controller to map to specific URLs,
Having problems with a small awk script, Im trying to choose the newest of
Having problems iterating. Problem has to do with const correctness, I think. I assume
Im having problems displaying records to my view when passing viewdata to a user
Im having problems building a query with the linq to sql data query expression

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.