not looking deeply but the hybrid you have in prop_3 doesn't seem to have > any relationship to the base set of rows you're getting from "fractions". > it returns multiple rows because statement2 isn't using any aggregates. > > How about a straight SQL string? what SQL do you expect? these are very > easy to link to a hybrid. > > It's difficult to issue a straight SQL string for the hybrid property itself because what I'm working with are group_by constructs which depend on aggregate functions. So, the hybrid property would need to be aware of its context. For example, in the code below, prop_3 now returns the maximum of all fractions and is not responsive to the group_by that comes later down the line:
ECHO = False from sqlalchemy.orm import sessionmaker, aliased from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, select, func from sqlalchemy.orm import relationship from sqlalchemy.ext.hybrid import hybrid_property engine = create_engine('sqlite:///:memory:', echo=ECHO) Base = declarative_base() class Fraction(Base): __tablename__ = 'fractions' id = Column(Integer, primary_key = True) prop_1 = Column(Integer) prop_2 = Column(Integer) prop_3_num = Column(Integer) prop_3_den = Column(Integer) @hybrid_property def prop_3(self) : return 0 @prop_3.expression def prop_3(self) : alias_1 = aliased(Fraction) alias_2 = aliased(Fraction) statement1 = select([func.max(1.0 * alias_1.prop_3_num / alias_1.prop_3_den).label('fmax')]) statement2 = select([alias_2.prop_3_num.label('prop_3_num_max'), alias_2.prop_3_den.label('prop_3_den_max')]).\ where((1.0 * alias_2.prop_3_num / alias_2.prop_3_den) == statement1) return statement2 Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session() for x in range(10) : session.add(Fraction(prop_1=x%2, prop_2=x%4, prop_3_num = x+1, prop_3_den=x+2)) session.commit() for x in session.query(Fraction.prop_1, Fraction.prop_2, Fraction.prop_3) : print x print "########################" for x in session.query(Fraction.prop_1, Fraction.prop_2, Fraction.prop_3).group_by(Fraction.prop_1, Fraction.prop_2) : print x ******* the result is: (0, 0, 10, 11) (1, 1, 10, 11) (0, 2, 10, 11) (1, 3, 10, 11) (0, 0, 10, 11) (1, 1, 10, 11) (0, 2, 10, 11) (1, 3, 10, 11) (0, 0, 10, 11) (1, 1, 10, 11) ######################## (0, 0, 10, 11) (0, 2, 10, 11) (1, 1, 10, 11) (1, 3, 10, 11) whereas I'd like for the local maxima to be chosen, meaning: (0, 0, 9, 10) (0, 2, 7, 8) (1, 1, 10, 11) (1, 3, 8, 9) -- 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 post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.