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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:37:19+00:00 2026-06-15T02:37:19+00:00

I have the classes Node and Leaf (Node) as shown below: It works fine,

  • 0

I have the classes Node and Leaf (Node) as shown below: It works fine, but I’d prefer to shift the leafs and subleafs definitions to the Leaf (Node) class. How do I achieve that?

class Node (db.Model):
    __mapper_args__ = {'polymorphic_identity':'node', 'polymorphic_on':'type'}
    id = db.Column (db.Integer, primary_key=True)
    type = db.Column ('type', db.String (16))

    root_id = db.Column (db.Integer, db.ForeignKey (id))
    nodes = db.relationship ('Node',
        cascade='all', lazy='dynamic',
        primaryjoin='Node.root_id==Node.id',
        backref=db.backref('root', remote_side=id))
    leafs = db.relationship ('Leaf',
        cascade='all', lazy='dynamic',
        primaryjoin='Leaf.root_id==Node.id')

    base_id = db.Column (db.Integer, db.ForeignKey (id))
    subnodes = db.relationship ('Node',
        cascade='all', lazy='dynamic',
        primaryjoin='Node.base_id==Node.id',
        backref=db.backref('base', remote_side=id))
    subleafs = db.relationship ('Leaf',
        cascade='all', lazy='dynamic',
        primaryjoin='Leaf.base_id==Node.id')

    def __init__ (self, root):
        self.base = root.base if root and root.base else root
        self.root = root

and

class Leaf (Node):
    __mapper_args__ = {'polymorphic_identity': 'leaf'}
    leaf_id = db.Column (db.Integer, db.ForeignKey ('node.id'), primary_key=True)

    def __init__ (self, root):
        super (Leaf, self).__init__ (root)

I tried this, but failed (partially):

class Leaf (Node):
    __mapper_args__ = {'polymorphic_identity': 'leaf'}
    leaf_id = db.Column (db.Integer, db.ForeignKey ('node.id'), primary_key=True)

    _x = db.relationship ('Node', backref=db.backref ('leafs',
        cascade='all', lazy='dynamic', primaryjoin='Leaf.root_id==Node.id'))
    _y = db.relationship ('Node', backref=db.backref ('subleafs',
        cascade='all', lazy='dynamic', primaryjoin='Leaf.base_id==Node.id'))

    def __init__ (self, root):
        super (Leaf, self).__init__ (root)

My delete test cases did not like this (just deleting the base/root node in the tree and relying on cascade='all'), and complained with:

CircularDependencyError: Circular dependency detected. Cycles: set([DeleteState(<Leaf at 0x22789d0>)]) all edges: set([(DeleteState(<Leaf at 0x22789d0>), DeleteState(<Leaf at 0x22789d0>))])

The reason I want to shift the definitions is, because I do not want to extend Node with definitions for every sub-class of Leaf (Node), which I might introduce later. Further, I definitely don’t need _x and _y, since I already have Leaf.root and Leaf.base (provided by Node); but omitting them (_x = & _y =) called for trouble like:

AttributeError: 'Node' object has no attribute 'leafs'

I guess I’m required to use something in Leaf (Node) to attach the relations to, even though I was not required to use any backrefs in my original definitions for leafs and subleafs in Node. Thx.

  • 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-15T02:37:20+00:00Added an answer on June 15, 2026 at 2:37 am

    Well, after some trying I’ve found a solution, that is not perfect, but does the job: I just moved the Node.leafs and Node.subleafs definitions to the file leaf.py where class Leaf (node) has been defined and appended with the former definitions like below:

    class Leaf (Node):
        __mapper_args__ = {'polymorphic_identity': 'leaf'}
        leaf_id = db.Column (db.Integer, db.ForeignKey ('node.id'), primary_key=True)
    
        def __init__ (self, root):
            super (Leaf, self).__init__ (root)
    
    Node.leafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
        primaryjoin=Node.id==Leaf.root_id)
    
    Node.subleafs = db.relationship (Leaf, cascade='all', lazy='dynamic',
        primaryjoin=Node.id==Leaf.base_id)
    

    A disadvantage of this is that people have to from models import Leaf if they want to access Node.leafs and Node.subleafs, but since they had to do this anyway (even if Node.leafs and Node.subleafs had been defined as backrefs within class Leaf (Node)), it is allright.

    If somebody finds a solution, where the relationships are defined as backrefs within class Leaf (Node), I’d be glad to hear from; thx.

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

Sidebar

Related Questions

I have this set of classes: Node (SUPER Class) |------ NodeType1 (Class) |----------NodeType2 (Class)
I have two classes Node and NodeContainer: class Node: public QObject { NodeContainer* parent;
I have classes structured like this: Public MustInherit Class A ' several properties End
Say I have classes class A{ //code for class A } class B{ //code
If I have classes of Type A and B: public class A { public
I tend to have this redundant naming in case classes: abstract class MyTree case
I have two classes, a base class and a derived class. My base class
I have a base 'node' class with an 'update()' member function now i have
I have a class (Node) which has a property of SubNodes which is a
Hi I have 2 classes in Java, Gossip and Node , I want that

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.