0

I'm attempting to make my first app with flask. This is my node class as of now:

class TreePage(db.Model):
    __tablename__ = 'tree'
    id = db.Column(db.Integer, primary_key= True)
    name = db.Column(db.String(100), nullable= False)
    content = db.Column(db.String)

    parent_id = db.create_all(db.Integer, db.ForeignKey('id'))

    children = db.relationship('TreeNode', cascade = "all",
    backref = db.backref("parent", remote_side='TreeNode.id'),
    collection_class = db.attribute_mapped_collection('name')
    )

Being new to SQLAlchemy and databases in general, lots of the information I'm reading here and on the SQLAlchemy documentation is kind of hard to grasp.

My goal right now is to be able to access a tree and its content, and then if that tree has children to be able to pick which child to study from; this is basically a visual representation of what I want to make.

My guess is this should be a one to many relationship, so I won't have to deal with local/remote sides right? Both of which confuse me at the moment. Next, do I need to worry at all about the cascade parameter, or can I leave it be for now?

Lastly, I want to solidify how self referential relationships work. With two different classes it's easy for me to grasp: the 'one' relationship creates a relationship object, and then the 'many' object creates a foreign key that references the primary key of its owner. But in the case of a self referential relationship, is the 'owner' the object that holds id and parent_id being the id of whatever parent object comes before? thanks and I apologize if this is at all confusing.

CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
Pouyan
  • 31
  • 3
  • 2
    http://docs.sqlalchemy.org/en/latest/orm/self_referential.html – nosklo Jul 19 '18 at 19:27
  • Application/business relation(ship)s/associations are represented by *tables*. A query returns rows of values participating in a relation(ship)/association per the rows of values participating in base/model relation(ship)s/associations. References aka "relation(ship)s" in the sense of *FKs* are not needed to query. They just declare constraints. What matters is what tables mean. [What is a self join for?](https://stackoverflow.com/a/37384306/3404097) Unfortunately ORMs obscure this because they muddle reference/FK & table declarations & directionality. – philipxy Jul 26 '18 at 13:08

0 Answers0