pony icon indicating copy to clipboard operation
pony copied to clipboard

Filtering by `orm.Set`

Open Real-Gecko opened this issue 8 months ago • 0 comments

I have following models

class Family(db.Entity):
    _table_ = "families"

    id = orm.PrimaryKey(int, auto=True)
    persons = orm.Set("Persons")

class Person(db.Entity):
    _table_ = "persons"

    id = orm.PrimaryKey(int, auto=True)
    family = orm.Required(Family, column="family_id")
    info = orm.Optional(orm.Json)

Now I need to filter Family object by persons where info meets certain criteria, like in following query:

select
  distinct(f.*)
FROM
  families f
  join persons p on p.family_id = f.id
where
  p.info ->> 'last_name' = 'Connor'

How can I achieve this with Pony if where clause is optional? I tried like this:

# filter may be "p.info ->> 'last_name' = 'Connor'" or maybe `None`
query = Family.select()
if filter:
    query.filter(lambda f: p for p in f.persons if raw_sql(filter))

But I get Undefined name f

Real-Gecko avatar Apr 29 '25 16:04 Real-Gecko