Sqlalchemy+elixir:如何查询多对多关系?
我正在将 sqlalchemy 与 Elixir 一起使用,并在尝试进行查询时遇到一些麻烦。
我有 2 个实体,Customer 和 CustomerList,具有多对多关系。
customer_lists_customers_table = Table('customer_lists_customers',
metadata,
Column('id', Integer, primary_key=True),
Column('customer_list_id', Integer, ForeignKey("customer_lists.id")),
Column('customer_id', Integer, ForeignKey("customers.id")))
class Customer(Entity):
[...]
customer_lists = ManyToMany('CustomerList', table=customer_lists_customers_table)
class CustomerList(Entity):
[...]
customers = ManyToMany('Customer', table=customer_lists_customers_table)
我正在尝试查找某个客户的 CustomerList:
customer = [...]
CustomerList.query.filter_by(customers.contains(customer)).all()
但出现错误: 名称错误:
未定义全局名称“customers”
客户似乎与实体字段无关,是否有一种特殊的查询形式可以处理关系(或多对多关系)?
谢谢
I'm using sqlalchemy with Elixir and have some troubles trying to make a query..
I have 2 entities, Customer and CustomerList, with a many to many relationship.
customer_lists_customers_table = Table('customer_lists_customers',
metadata,
Column('id', Integer, primary_key=True),
Column('customer_list_id', Integer, ForeignKey("customer_lists.id")),
Column('customer_id', Integer, ForeignKey("customers.id")))
class Customer(Entity):
[...]
customer_lists = ManyToMany('CustomerList', table=customer_lists_customers_table)
class CustomerList(Entity):
[...]
customers = ManyToMany('Customer', table=customer_lists_customers_table)
I'm tryng to find CustomerList with some customer:
customer = [...]
CustomerList.query.filter_by(customers.contains(customer)).all()
But I get the error:
NameError:
global name 'customers' is not defined
customers seems to be unrelated to the entity fields, there's an special query form to work with relationships (or ManyToMany relationships)?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用常规过滤器:
query.filter(CustomerList.customers.contains(customer))
。有关更多示例,请参阅 SQLAlchemy 文档。实际上filter_by是一个特例。query.filter_by(**kwargs)
简写仅适用于简单的相等比较。在底层query.filter_by(foo="bar", baz=42)
被委托给query.filter(and_(MyClass.foo == "bar", MyClass. baz == 42))
。 (实际上,要弄清楚您拥有多个实体的哪个属性,实际上有更多的魔力,但它仍然使用简单的委托)You can use regular filter:
query.filter(CustomerList.customers.contains(customer))
. See SQLAlchemy documentation for more examples. It's actually filter_by that's a special case. Thequery.filter_by(**kwargs)
shorthand works only for simple equality comparisons. Under the coverquery.filter_by(foo="bar", baz=42)
is delegated to the equivalent ofquery.filter(and_(MyClass.foo == "bar", MyClass.baz == 42))
. (There's actually slightly more magic to figure out which property you meant you have many entities, but it still uses simple delegation)请仔细阅读错误消息,它指出了问题的根源。你的意思
CustomerList.query.filter_by(CustomerList.customers.contains(customer)).all()
?更新:使用声明性定义时,您可以在类范围内使用刚刚定义的关系,但这些属性在类外部不可见:
Read the error message with attention, it points to the source of problem. Did you mean
CustomerList.query.filter_by(CustomerList.customers.contains(customer)).all()
?Update: When using declarative definition you can use just defined relation in class scope, but these properties are not visible outside class:
CustomerList.query.filter_by(CustomerList.customers.contains(customer)).all()
应该可以正常工作。CustomerList.query.filter_by(CustomerList.customers.contains(customer)).all()
should work fine.