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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:28:40+00:00 2026-05-23T14:28:40+00:00

Using the SqlAlchemy ORM, I’ve been trying to setup a one-to-many relationship between a

  • 0

Using the SqlAlchemy ORM, I’ve been trying to setup a one-to-many relationship between a parent table that has “child” records in a child table. The table and mapper declarations are below. The problem is, only one child record is read from the DB into the “children” attribute when there should be many records.

child_table = Table('child_table', metadata_obj,
                    Column('field_1', Integer, ForeignKey('parent_table.field_1'),
                           primary_key=True),
                    Column('field_2', String, ForeignKey('parent_table.field_2'),
                           primary_key=True),
                    autoload=True
mapper(Parent_Class, parent_table, properties={
       'children': relation(Child_Class, lazy=False, # Eager load
                            primaryjoin=and_(child_table.c.field_1==parent_table.c.field_1,
                                             parent_table.c.field_2==parent_table.c.field_2),
                            backref="parents"),
                        })
  • 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-23T14:28:40+00:00Added an answer on May 23, 2026 at 2:28 pm

    The problem was in the child_table definition. I had only defined the fields with a foreign key relationship as being part of the primary key for the child_table. However, those fields (field_1 & field_2) weren’t the complete primary key. So, it appears that SQLAlchemy was only loading one row. I added field_3 and field_4 to fully define the primary key for the child_table and then SQLAlchemy successfully read all records.

    child_table = Table('child_table', metadata_obj,
                        Column('field_1', Integer, ForeignKey('parent_table.field_1'),
                               primary_key=True),
                        Column('field_2', String, ForeignKey('parent_table.field_2'),
                               primary_key=True),
                        Column('field_3', String, primary_key=True),
                        Column('field_4', String, primary_key=True),
                        autoload=True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using __init__() like this in some SQLAlchemy ORM classes that have many parameters
I've been trying to map an object to a database using SQLAlchemy but have
How do I select one or more random rows from a table using SQLAlchemy?
I'm using SQLAlchemy as the ORM within an application i've been building for some
I am using Python 2.6.6 and SQLAlchemy 0.6.6 to handle a one to many
I'm using the ORM side of SQLAlchemy, and I've defined one of my columns
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
I'm using an ORM (SQLAlchemy, but my question is quite implementation-agnostic) to model a
Hi im using sqlalchemy on a db2 table with 500k rows. using plain sql
I have been using django ORM, it's nice and very easy, but this time

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.