【问题标题】:Pony ORM how to query in many-many relation tablePony ORM如何在多对多关系表中查询
【发布时间】:2020-08-24 18:09:39
【问题描述】:

Pony 实体关系定义如下。 多对多实体关系定义如下。 每个国家可以有多个公司,每个公司可以在多个国家,因此选择了多对多的关系。

class Country(database.Entity):

    country_code = orm.PrimaryKey(str)
    country_name = orm.Required(str, unique=True)
    companies = orm.Set('Company', reverse='countries')

class Company(database.Entity):

    company_id = orm.PrimaryKey(str)
    company_name = orm.Required(str, unique=True)
    countries = orm.Set(Country, reverse='companies')

假设A公司在X、Y国家,B公司在X、Y、Z,C公司在X

在上述情况下,我想过滤在 X、Y 国家注册的所有公司 结果应该是A公司和B公司

【问题讨论】:

    标签: python ponyorm


    【解决方案1】:

    在小马作者的支持下,我的问题得到了解决。 这是提供的解决方案,并且效果很好。

    query = orm.select(s for s in Company)
    query = query.filter(lambda s: orm.exists(country for country in s.countries if country.country_code in countries))
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2019-04-22
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 2010-12-11
      • 2013-11-22
      • 2011-11-13
      相关资源
      最近更新 更多