Sqlalchemy+elixir:如何查询多对多关系?

发布于 2024-08-31 17:47:46 字数 976 浏览 7 评论 0原文

我正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦亿 2024-09-07 17:47:46

您可以使用常规过滤器: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. The query.filter_by(**kwargs) shorthand works only for simple equality comparisons. Under the cover query.filter_by(foo="bar", baz=42) is delegated to the equivalent of query.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)

写给空气的情书 2024-09-07 17:47:46

请仔细阅读错误消息,它指出了问题的根源。你的意思
CustomerList.query.filter_by(CustomerList.customers.contains(customer)).all()

更新:使用声明性定义时,您可以在范围内使用刚刚定义的关系,但这些属性在类外部不可见:

class MyClass(object):
    prop1 = 'somevalue'
    prop2 = prop1.upper() # prop1 is visible here

val2 = MyClass.prop1 # This is OK    

val1 = prop1.lower() # And this will raise NameError, since there is no 
                     # variable `prop1` is global scope

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:

class MyClass(object):
    prop1 = 'somevalue'
    prop2 = prop1.upper() # prop1 is visible here

val2 = MyClass.prop1 # This is OK    

val1 = prop1.lower() # And this will raise NameError, since there is no 
                     # variable `prop1` is global scope
向日葵 2024-09-07 17:47:46

CustomerList.query.filter_by(CustomerList.customers.contains(customer)).all() 应该可以正常工作。

CustomerList.query.filter_by(CustomerList.customers.contains(customer)).all() should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文