I have a problem with ordering by hybrid_property, I create expression like in documentation, but is not sorted with sqlalchemy_utils.functions.sort_query
@hybrid_property
def organization_name(self):
if self.is_hq_group():
return self.name
elif self.is_campus_group():
return None
else:
org_obj = (
Group.query.filter(
Group.type == GroupType.GREEK_ORGANIZATION,
)
.filter(Group.children.any(id=self.id))
.first()
)
return org_obj.name
@organization_name.expression
def organization_name(cls):
return select([Group.name]). \
where(Group.type == GroupType.GREEK_ORGANIZATION). \
where(Group.children.any(id=cls.id)). \
limit(1)
But the query comes not sorted, without an exception... Here are 2 tables (M2M relation)
class GroupChildren(db.Model):
"""
Through table that links a parent and child group.
"""
__tablename__ = "group_children"
# Group relationship
group_id = db.Column(
db.Integer, db.ForeignKey("group.id", ondelete="CASCADE"), primary_key=True
)
child_id = db.Column(
db.Integer,
db.ForeignKey("group.id", ondelete="CASCADE"),
primary_key=True,
)
is_dashboard_linked = db.Column(db.Boolean, nullable=False, default=False)
Main Table have a Many-to-many relation with them-self
class Group(BaseModel):
"""A group of users with a name/identity."""
query_class = GroupQuery
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
type = db.Column(ChoiceType(GroupType, impl=db.Integer()))
# Deleting a parent or a child will not delete associated groups.
children = db.relationship(
"Group",
secondary="group_children",
backref=db.backref("parents", lazy="dynamic"),
passive_deletes=True,
lazy="dynamic",
primaryjoin="Group.id == group_children.c.group_id",
secondaryjoin="Group.id == group_children.c.child_id",
)