【问题标题】:Using sqlalchemy.sql.functions.char_length as a filter condition使用 sqlalchemy.sql.functions.char_length 作为过滤条件
【发布时间】:2011-09-29 19:27:29
【问题描述】:

我正在使用 Elixir 和 sqla 0.6,我正在尝试查询我的模型:

class Document(Entity): 
    using_options(shortnames=True, order_by='doc_date')
    doc_number = Field(Unicode(20),index=True)

...对于具有给定长度的数字的文档。

我在想这样的事情:

Document.query.filter(Document.doc_number.char_lenght()==5).all()

...但显然,char_length 虽然存在于 sqlalchemy.sql.functions 中,但在这里不起作用。如何在不使用直接查询的情况下使其在声明式惯用语中工作?

【问题讨论】:

  • 只是一个建议:在下面发布您的答案,并接受它作为答案,以便人们立即知道您的问题已解决
  • @kusut 我正在尝试按照您的建议进行操作,但代码标记在评论编辑器中似乎不起作用,我对界面感到困惑......我会再试一次,谢谢.

标签: python sqlalchemy python-elixir


【解决方案1】:

好的,找到答案了:

from sqlalchemy import func
Document.query.filter(func.char_length(Document.doc_number)==5).all()

【讨论】:

  • 我猜它是在更新的版本中移动的,但现在在 sqlalchemy.sql.functions.char_length
猜你喜欢
  • 1970-01-01
  • 2020-08-17
  • 2021-09-18
  • 2015-10-12
  • 1970-01-01
  • 2019-01-26
  • 2021-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多