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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:50:05+00:00 2026-05-23T23:50:05+00:00

Basically, I have this model, where I mapped in a single table a BaseNode

  • 0

Basically, I have this model, where I mapped in a single table a “BaseNode” class, and two subclasses. The point is that I need one of the subclasses, to have a one-to-many relationship with the other subclass.
So in sort, it is a relationship with another row of different class (subclass), but in the same table.
How do you think I could write it using declarative syntax?.

Note: Due to other relationships in my model, if it is possible, I really need to stick with single table inheritance.

class BaseNode(DBBase):
    __tablename__ = 'base_node'
    id = Column(Integer, primary_key=True)
    discriminator = Column('type', String(50))
    __mapper_args__ = {'polymorphic_on': discriminator}

class NodeTypeA(BaseNode):
    __mapper_args__ = {'polymorphic_identity': 'NodeTypeA'}
    typeB_children = relationship('NodeTypeB', backref='parent_node')


class NodeTypeB(BaseNode):
    __mapper_args__ = {'polymorphic_identity': 'NodeTypeB'}
    parent_id = Column(Integer, ForeignKey('base_node.id'))

Using this code will throw:

sqlalchemy.exc.ArgumentError: NodeTypeA.typeB_children and
back-reference NodeTypeB.parent_node are both of the same direction
. Did you mean to set remote_side on the
many-to-one side ?

Any ideas or suggestions?

  • 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-23T23:50:06+00:00Added an answer on May 23, 2026 at 11:50 pm

    I was struggling through this myself earlier. I was able to get this self-referential relationship working:

    class Employee(Base):
      __tablename__ = 'employee'
      id = Column(Integer, primary_key=True)
      name = Column(String(64), nullable=False)
    Employee.manager_id = Column(Integer, ForeignKey(Employee.id))
    Employee.manager = relationship(Employee, backref='subordinates',
        remote_side=Employee.id)
    

    Note that the manager and manager_id are “monkey-patched” because you cannot make self-references within a class definition.

    So in your example, I would guess this:

    class NodeTypeA(BaseNode):
        __mapper_args__ = {'polymorphic_identity': 'NodeTypeA'}
        typeB_children = relationship('NodeTypeB', backref='parent_node',
            remote_side='NodeTypeB.parent_id')
    

    EDIT: Basically what your error is telling you is that the relationship and its backref are both identical. So whatever rules that SA is applying to figure out what the table-level relationships are, they don’t jive with the information you are providing.

    I learned that simply saying mycolumn=relationship(OtherTable) in your declarative class will result in mycolumn being a list, assuming that SA can detect an unambiguous relationship. So if you really want an object to have a link to its parent, rather than its children, you can define parent=relationship(OtherTable, backref='children', remote_side=OtherTable.id) in the child table. That defines both directions of the parent-child relationship.

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

Sidebar

Related Questions

I have 2 tables that are mapped in my entity model which are basically
I have this error that is keeping me from moving forward. I basically have
Well basically I have this script that takes a long time to execute and
I have this script which basically toggles a bgColor class on and off so
Basically, I have this webpage that is the header on any page you go
Basically you have two ways for doing this: for (int x = 0; x
I have a class that I am binding to my view model. It is
Suppose I have this model (not real code): class Message(models.Model): content = models.CharField(...) mentions
I basically have something like this: void Foo(Type ty) { var result = serializer.Deserialize<ty>(inputContent);
I have this grand idea to basically employ some brute force attack to test/verify

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.