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

  • Home
  • SEARCH
  • 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 7087517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:41:01+00:00 2026-05-28T07:41:01+00:00

In SQLAlchemy, imagine we have a table Foo with a compound primary key, and

  • 0

In SQLAlchemy, imagine we have a table Foo with a compound primary key, and Bar, which has two foreign key constrains linking it to Foo (each Bar has two Foo objects).

My problem is with the relationship function, which makes me repeat the information (in the primaryjoin) which I have already given in the ForeightKeyConstraint (violating DRY). Is there some other way of structuring this so that I don’t have to repeat information? Some way of passing the relevant ForeignKeyConstraint to relationship?

class Foo(Base):
    __tablename__ = "Foo"
    id_1 = Column(Integer, primary_key=True)
    id_2 = Column(Integer, primary_key=True)

class Bar(Base):
    __tablename__ = "Bar"
    id = Column(Integer, primary_key=True)

    foo_1_id_1 = Column(Integer)
    foo_1_id_2 = Column(Integer)

    foo_2_id_1 = Column(Integer)
    foo_2_id_2 = Column(Integer)

    __table_args__ = (
            ForeignKeyConstraint(
                [foo_1_id_1,foo_1_id_2],
                [Foo.id_1,Foo.id_2]
                ),
            ForeignKeyConstraint(
                [foo_2_id_1,foo_2_id_2],
                [Foo.id_1,Foo.id_2]
                )
            )

    foo_1 = relationship(Foo,primaryjoin="(Bar.foo_1_id_1 == Foo.id_1) & (Bar.foo_1_id_2 == Foo.id_2)")
    foo_2 = relationship(Foo,primaryjoin="(Bar.foo_2_id_1 == Foo.id_1) & (Bar.foo_2_id_2 == Foo.id_2)")

Thanks.

  • 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-28T07:41:02+00:00Added an answer on May 28, 2026 at 7:41 am

    The relationship(), as it is, cannot determine its full configuration. This is always the case when there is more than one way to refer to the related table.
    In your example it seems like sqlalchemy could be smart enough to guess by the names of the columns, but this is not what does and it should not.

    Although it might seem as if you have information repetition, in fact you are just being specific about your relationship configuration.

    There is, in fact, option to specify foreign_keys in the relationship() configuration, but currently is serves somewhat different purpose, so you still will need to configure primaryjoin.

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

Sidebar

Related Questions

In sqlalchemy 0.5 i have a table defined like this one: orders = Table('orders',
I have table in sqlalchemy 0.4 that with types.DateTime column: Column(dfield, types.DateTime, index=True) I
I have a SQLAlchemy model with a one-to-many relationship between table x and table
I have some SQLAlchemy objects which contain lists of more SQLAlchemy objects, and so
Using SQLAlchemy , I have a one to many relation with two tables -
I have an SqlAlchemy table like so: table = sql.Table('treeItems', META, sql.Column('id', sql.Integer(), primary_key=True),
I don't have much experience with SQLAlchemy and I have a problem, which I
I'm new to SQLAlchemy. I currently have: ev = model.EnumerationValue(key=key_level_2, code=level_2) ev.keyvalues[key_parent] = level_1
I'm using SQLAlchemy, and many classes in my object model have the same two
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class

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.