Trying to understand the right way to define a hybridproperty.expression 
that references an 'sub-attribute' of a relationship attribute.  I have 
done it in another case by using the class name of the relationship 
attribute (Tire.weight vs. cls.tire.weight) and that's worked OK.  But it 
begs the question how to do it if that class (Tire) is used in multiple 
relationship attributes.  How does the .expression discern between the two.

Hopefully the example below explains better than my words probably have.  
See the final .expression "weight_of_tires"... how to make it reference the 
front_tire attribute and rear_tire attribute - both of the same model class 
- distinctly?  This sample is using flask-alchemy - hopefully that doesn't 
confuse things.

class Tire(db.Model):
    __tablename__ = 'tire'
    id = Column(Integer, primary_key=True)
    weight = Column(Integer)
    size = Column(VARCHAR(50))

class Motorcycle(db.Model):
    __tablename__ = 'motorcycle'
    id = Column(Integer, primary_key=True)
    front_tire_id = Column(ForeignKey(Tire.id))
    rear_tire_id = Column(ForeignKey(Tire.id))

    front_tire = relationship(Tire, foreign_keys=[front_tire_id], 
lazy='joined')
    rear_tire = relationship(Tire, foreign_keys=[rear_tire_id], 
lazy='joined')

    @hybrid_property
    def weight_of_tires(self):
        return self.front_tire.weight + self.rear_tire.weight

    @weight_of_tires.expression
    def weight_of_tires(cls):
        # return Tire.weight + Tire.weight
        # return cls.front_tire.weight + cls.rear_tire.weight

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/3e4cd06e-8d7f-4471-81f9-6ecbf8f236ban%40googlegroups.com.

Reply via email to