librelist archives

« back to archive

Nested recursive lazy joins with Flask-SQLAlchemy

Nested recursive lazy joins with Flask-SQLAlchemy

From:
Franklyn Tackitt
Date:
2013-08-09 @ 18:00
I have a User object, and a Groups object. Groups can have children (1
level only). I'm trying to get them to auto-join the children when the user
object is pulled, but its doing multiple requests to get the children of
each group when I iterate through them. Here are my models so far (trimmed
down). Any idea how to fix this?


> user_manager_groups_relationship =
> ManyToMany("user_manager_groups_relationship", "users.id", "
> managergroup.id")
> class ManagerGroup(db.Model):
> id = db.Column(db.Integer, primary_key=True, index=True)
> parent_id = db.Column(db.Integer, db.ForeignKey("managergroup.id"),
> nullable=True, index=True)
> parent = db.relationship("ManagerGroup", backref="children",
> remote_side=id, lazy="joined")
> name = db.Column(db.String(80), nullable=False)
> class User(db.Model, Serializable):
> managergroups = db.relationship("ManagerGroup",
> secondary=user_manager_groups_relationship, lazy="joined")