简体   繁体   中英

SQLAlchemy chaining association proxy for great grandchildren?

I have a four classes like so: Group , Parent , Child , Toy .

  • Group has a parents relationship pointing to Parent
  • Parent has a children relationship pointing to Child
  • Child has a toys relationship pointing to Toy

Parent has a toys association_proxy that produces all the Toy s that the Parent 's children have.

I want to be able to get all the Toys in a Group. I tried to create an association_proxy on Group that links to Parent 's toys , but it produces this:

[[<Toy 1>, <Toy 2>], [], [], []]

when I want this:

[<Toy 1>, <Toy 2>]

If the Parent 's children don't have any Toy s, then the toys association proxy is [] . However, the second association proxy doesn't know to exclude the empty lists. Also, the lists should be collapsed. Is there anyway to get this to work?

class Group(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime, default=utils.get_now_datetime)
    name = db.Column(db.String(80, convert_unicode=True))
    # relationships
    parents = db.relationship('Parent', backref='group')
    toys = association_proxy('parents', 'toys')

class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    group_id = db.Column(db.Integer, db.ForeignKey('group.id', ondelete='CASCADE'))
    created_at = db.Column(db.DateTime, default=utils.get_now_datetime)
    first_name = db.Column(db.String(80, convert_unicode=True))
    last_name = db.Column(db.String(80, convert_unicode=True))
    children = db.relationship('Child', backref='parent', cascade='all, delete')
    toys = association_proxy('children', 'toys')

class Child(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, db.ForeignKey('parent.id', ondelete='CASCADE'))
    created_at = db.Column(db.DateTime, default=utils.get_now_datetime)

class Toy(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    child_id = db.Column(db.Integer, db.ForeignKey('child.id', ondelete='CASCADE'))
    created_at = db.Column(db.DateTime, default=utils.get_now_datetime)
    child = db.relationship('Child', backref=db.backref("toys",cascade="all, delete-orphan", order_by="desc(Toy.id)"))

Given that those are for retrieval and view only (as you mentioned in the comment, adding would be ambiguous), I would rather do a viewonly relationship without an association_proxy :

class Group(db.Model):
    # ...
    toys = relationship('Toy',
        secondary="join(Group, Parent, Group.id == Parent.group_id).join(Child, Parent.id == Child.parent_id)",
        primaryjoin="and_(Group.id == Parent.group_id, Parent.id == Child.parent_id)",
        secondaryjoin="Child.id == Toy.child_id",
        viewonly=True,
    )

Note that this is a new feature of SQLAlchemy and is describe in the Composite “Secondary” Joins section of the documentation.

Then you can use it just for query:

group_id = 123
group = session.query(Group).get(group_id)
print(group.toys)

Or you can even use it to filter, so to find a group which contains a toy with name "Super Mario" you can do:

group = session.query(Group).filter(Group.toys.any(Toy.name == "Super Mario"))

But in reality all this you can do with simple query, or create a query-enabled property. See Customizing Column Properties section of the documentation, where you can use any of the simple property, column_property or hybrid attribute .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM