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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:20:14+00:00 2026-06-13T02:20:14+00:00

I want to have a parent child relationship in the same table. Below is

  • 0

I want to have a parent child relationship in the same table. Below is what I have, and it isn’t working. By “not working”, I meant that when I call .children on some Item object, it gives me []. What is the error in my code and how to do it correctly? Thanks.

parents_children = Table('parents_children', Base.metadata,
                         Column('parent_id', bigint, ForeignKey('items.id', name='a'), primary_key=True),
                         Column('child_id', bigint, ForeignKey('items.id', name='b'), primary_key=True)
                         )

class Item(Base):
    __tablename__ = 'items'
    id = Column(bigint, primary_key=True, autoincrement=True)
    name = Column(types.String(80), default="")
    display_name = Column(types.String(80), default="")
    version = Column(types.String(80), default="")
    discriminator = Column(item_tag_enum, nullable=False)
    children = relationship('Item',
                            secondary=parents_children,
                            primaryjoin=id == parents_children.c.parent_id,
                            secondaryjoin=id == parents_children.c.child_id,
                            backref='parents')
    __mapper_args__ = {'polymorphic_on' : discriminator}

 # Item subclasses omitted because that's irrelevant to the question.
  • 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-13T02:20:15+00:00Added an answer on June 13, 2026 at 2:20 am

    I think there is an easier way… (this is how I do it)

    def adopt(parent_id,child_id):
        parent = DBSession.query(Item).get(parent_id)
        child  = DBSession.query(Item).get(child_id)
        parent.children.append(child)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have django model which consist of parent child relationship filed I want to
I have two tables Loan(Parent Table) and Receipt(Child table)what i want to do is
I have several child windows, all with the same parent. I want to cascade/tile
I have some Oracle tables that represent a parent-child relationship. They look something like
I have a pair of tables with parent-child relationship. domain: id int not null
Suppose I have two tables in a parent - child relationship. Let's call them
i have a table in sql that uses the adjacency model to create child/parent
I have got a couple of tables with a parent child relationship. I want
I have two tables with parent - child relationship: PARENT table with id as
I have a Standard table, which sotres parent, child category relationship...like this. id, parent,

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.