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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:00:06+00:00 2026-05-21T22:00:06+00:00

I have a database model where I need a one-to-many relationship and two one-to-one

  • 0

I have a database model where I need a one-to-many relationship and two one-to-one relationships. Here’s the model i’ve made, but it’s throwing errors

class Page(Base):
    __tablename__ = 'pages'
    id          = Column(Integer, primary_key=True)
    title       = Column(String(100), nullable=False)
    content     = Column(Text, nullable=False)

    parent_id   = Column(Integer, ForeignKey("pages.id"), nullable=True)
    children    = relationship("Page", backref=backref("parent", remote_side=id))

    next_id     = Column(Integer, ForeignKey("pages.id"), nullable=True)
    next        = relationship("Page", backref=backref("prev", remote_side=id, uselist=False))

    prev_id     = Column(Integer, ForeignKey("pages.id"), nullable=True)
    prev        = relationship("Page", backref=backref("next", remote_side=id, uselist=False))

    def __init__(self, title, content, parent_id=None, next_id=None, prev_id=None):
        self.title = title
        self.content = content
        self.parent_id = parent_id
        self.next_id = next_id
        self.prev_id = prev_id

    def __repr__(self):
        return '<Page "%r">' % self.title

I get the following error whenever i try to do anything to the database

ArgumentError: Could not determine join condition between parent/child tables on relationship Page.children. Specify a 'primaryjoin' expression. If 'secondary' is present, 'secondaryjoin' is needed as well.

What’s really weird is that it worked without the next and prev columns. Anybody know what’s wrong?

  • 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-21T22:00:07+00:00Added an answer on May 21, 2026 at 10:00 pm

    The topic is old, but since this is so confusing i’ll write it down.
    You don’t need separate ‘prev’ column, you already have it as backref for ‘next’.
    Also, since you have multiple foreign keys to the same target, you need to specify primary joins manually:

    class Page(Base):
        __tablename__ = 'pages'
        id          = Column(Integer, primary_key=True)
        title       = Column(String(100), nullable=False)
        content     = Column(Text, nullable=False)
    
        parent_id   = Column(Integer, ForeignKey("pages.id"), nullable=True)
        parent      = relationship("Page",
                        primaryjoin=('pages.c.id==pages.c.parent_id'),
                        remote_side='Page.id',
                        backref=backref("children" ))
    
        next_id     = Column(Integer, ForeignKey("pages.id"), nullable=True)
        next        = relationship("Page", 
                        primaryjoin=('pages.c.next_id==pages.c.id'),
                        remote_side='Page.id', 
                        backref=backref("prev", uselist=False))
    

    A couple of bugs or just bits of weird behavior i noticed:
    – You can only use remote_side="Page.id", not remote_side=[id] and not remote_side=["Page.id"], or it won’t work (sqlalchemy 0.6.6). This was annoying to pin down.
    – It seem like you should always use remote_side with primary key, regardless of what you actual remote side is. remote_side="Pages.next_id" will always generate a weird error, even if it seems appropriate.
    – primaryjoin expression is confusing as hell, as it doesn’t use aliases, but this is actually the correct way to do it. The binding engine knows which expression to replace with a parameter (which is way too implicit and against the Zen, btw).

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

Sidebar

Related Questions

Here is a simplified version of my database model. I have two tables: Image,
I have two separate sets of tables in the same database that model the
I have a model withs Parents, Children and Grandchildren, in a many-to-many relationship. Using
I designed a database using MySQLWorkbench EER model with many foreign key relationships and
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have a Deal model and one Category model having many to many mapping
I have 3 database model - Semester, Section and Notecard The Notecard model has
I have a model on top of my database model and map the objects
I have a real entity Division under database model (EF 4.0). Also I have
I have category model that has a tree structure. In my database I have

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.