'Hybrid expression for relationship of relationship

I have three models linked by a relationship chain. An entry has requests and a partner. So every request has a related partner by this:

class Request(db.Model):
    __tablename__ = "requests"

    id = db.Column(UUID, primary_key=True, default=uuid.uuid4)

    entry_id = db.Column(
        UUID,
        db.ForeignKey("entries.id", name="requests_entry_fk"),
        nullable=False,
    )
    entry = db.relationship("Entry", back_populates="requests")


class Entry(db.Model):
    __tablename__ = "entries"

    id = db.Column(UUID, primary_key=True, default=uuid.uuid4)

    requests = db.relationship(
        "Request", back_populates="entry", lazy="dynamic"
    )
    partner_id = db.Column(
        UUID, db.ForeignKey("partners.id"), nullable=False
    )
    partner = db.relationship("Partner", back_populates="entries")


class Partner(db.Model):
    __tablename__ = "partners"

    id = db.Column(UUID, primary_key=True, default=uuid.uuid4)

    entries = db.relationship("Entry", back_populates="partner", lazy="dynamic")

On the Request model I'd like to add the Partner as well but since the Entry already relates to it I figured a simple hybrid_property will suffice:

@hybrid_property
def partner(self):
    return self.entry.partner

@partner.expression
def partner(cls):
    return select([Entry.partner]).where(cls.entry_id == Entry.id)

But the expression does not seem to work at all:

Neither 'hybrid_property' object nor 'ExprComparator' object associated with Request.partner has an attribute 'id'

What am I missing here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source