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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:43:12+00:00 2026-06-10T17:43:12+00:00

In SQLAlchemy, I would like to write a set of records in table A.

  • 0

In SQLAlchemy, I would like to write a set of records in table A. Then in table B I want to write a record that references an (a priori) unknown number of the records in A.

In python terms, I would create classes A and B and then have a list in B that contains objects of type A.

Can that be done?

  • 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-06-10T17:43:13+00:00Added an answer on June 10, 2026 at 5:43 pm

    This is not really a SQLAlchemy question, but a basic relational tables question.

    You are either talking about a one-to-many relationship, or a many-to-many relationship, both of which SQLAlchemy supports out-of-the-box.

    One-to-many

    A one-to-many relationship is one of basic containment; a department has many employees, but each employee has only one department. The class with only one outgoing relationship gets the foreign key in this case, so employees refer to departments, not the other way around.

    The example from the SQLAlchemy documentation is given as:

    class Parent(Base):
        __tablename__ = 'parent'
        id = Column(Integer, primary_key=True)
        children = relationship("Child")
    
    class Child(Base):
        __tablename__ = 'child'
        id = Column(Integer, primary_key=True)
        parent_id = Column(Integer, ForeignKey('parent.id'))
    

    So if your A classes can only be part of one B set, use this pattern.

    Many-to-many

    A many-to-many relationship is one where both sides can refer to more than one instance of the other side. Think of employees and projects; an employee can be part of multiple projects, and a project can involve multiple employees.

    In SQL, this kind of relationship requires an association table, an extra database table that holds the mapping between the two linked types. The SQLAlchemy documentation on many-to-many relationships documents clearly how to do this; here is the example:

    association_table = Table('association', Base.metadata,
        Column('left_id', Integer, ForeignKey('left.id')),
        Column('right_id', Integer, ForeignKey('right.id'))
    )
    
    class Parent(Base):
        __tablename__ = 'left'
        id = Column(Integer, primary_key=True)
        children = relationship("Child",
                        secondary=association_table)
    
    class Child(Base):
        __tablename__ = 'right'
        id = Column(Integer, primary_key=True)
    

    Use this pattern if each A object can be part of more than one B set.

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

Sidebar

Related Questions

I would like function accept a class that has inherited SQLAlchemy's declarative_base, and it
I would like to set a column default value that is based on another
I've got a sqlalchemy model that is set up like this: class Entry(Base): __tablename__
I would like to document a set of sqlalchemy database classes. I have followed
I really like the feature of SQLAlchemy that allows you to see if an
Given a SQLAlchemy mapped class Table and an instance of that class t ,
I am using SQLAlchemy and would like to log executed SQL code (i.e. the
I am creating SQLAlchemy class that represents user credentials. I want to have field
Does SQLAlchemy support H2 db? I'm using pyramid and would like to connect to
I'm trying to create a social-network like many-to-many mapping in SQLAlchemy. That is I

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.