Skip to content

Filtering by orm.Set #746

@Real-Gecko

Description

@Real-Gecko

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions