Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pony ORM如何查询多个关系表_Python_Ponyorm - Fatal编程技术网

Python Pony ORM如何查询多个关系表

Python Pony ORM如何查询多个关系表,python,ponyorm,Python,Ponyorm,小马实体关系定义如下。 多对多实体关系定义如下。 每个国家可以有多个公司,每个公司可以在多个国家,因此选择了多对多关系 class Country(database.Entity): country_code = orm.PrimaryKey(str) country_name = orm.Required(str, unique=True) companies = orm.Set('Company', reverse='countries') class Compa

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

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公司

在小马作家的支持下,我得到了问题的解决方案。 下面是提供的解决方案,它运行良好

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))