SqlAlchemy 2.0 中文文档(二十九)(2)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云原生数据库 PolarDB PostgreSQL 版,企业版 4核16GB
推荐场景:
HTAP混合负载
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: SqlAlchemy 2.0 中文文档(二十九)

SqlAlchemy 2.0 中文文档(二十九)(1)https://developer.aliyun.com/article/1560463


参数:

other – 一个文字列表,一个select()构造,或者一个包含设置为 True 的bindparam()构造,其中包括bindparam.expanding标志。

attribute info
method is_(other: Any) → ColumnOperators

继承自 ColumnOperators.is_() 方法的 ColumnOperators

实现IS运算符。

通常,当与None的值进行比较时,IS会自动生成,解析为NULL。但是,在某些平台上,如果要与布尔值进行比较,则可能希望显式使用IS

另请参阅

ColumnOperators.is_not()

method is_distinct_from(other: Any) → ColumnOperators

继承自 ColumnOperators.is_distinct_from() 方法的 ColumnOperators

实现IS DISTINCT FROM运算符。

在大多数平台上呈现“a IS DISTINCT FROM b”;在某些平台上,如 SQLite,可能呈现“a IS NOT b”。

method is_not(other: Any) → ColumnOperators

继承自 ColumnOperators.is_not() 方法的 ColumnOperators

实现IS NOT运算符。

通常,当与None的值进行比较时,IS NOT会自动生成,解析为NULL。但是,在某些平台上,如果要与布尔值进行比较,则可能希望显式使用IS NOT

在 1.4 版本中更改:is_not()运算符从先前版本的isnot()重命名。以前的名称仍可用于向后兼容。

另请参阅

ColumnOperators.is_()

method is_not_distinct_from(other: Any) → ColumnOperators

继承自 ColumnOperators.is_not_distinct_from() 方法的 ColumnOperators

实现 IS NOT DISTINCT FROM 操作符。

在大多数平台上呈现“a IS NOT DISTINCT FROM b”;在某些平台上,如 SQLite,可能呈现“a IS b”。

从版本 1.4 开始更改:is_not_distinct_from() 操作符从先前版本的 isnot_distinct_from() 重命名。以确保向后兼容性,先前的名称仍然可用。

method isnot(other: Any) → ColumnOperators

继承自 ColumnOperators.isnot() 方法的 ColumnOperators

实现 IS NOT 操作符。

通常,当与 None 的值进行比较时,会自动生成 IS NOT,这将解析为 NULL。但是,如果在某些平台上与布尔值进行比较时,可能希望显式使用 IS NOT

从版本 1.4 开始更改:is_not() 操作符从先前版本的 isnot() 重命名。��确保向后兼容性,先前的名称仍然可用。

另请参见

ColumnOperators.is_()

method isnot_distinct_from(other: Any) → ColumnOperators

继承自 ColumnOperators.isnot_distinct_from() 方法的 ColumnOperators

实现 IS NOT DISTINCT FROM 操作符。

在大多数平台上呈现“a IS NOT DISTINCT FROM b”;在某些平台上,如 SQLite,可能呈现“a IS b”。

从版本 1.4 开始更改:is_not() 操作符从先前版本的 isnot() 重命名。以确保向后兼容性,先前的名称仍然可用。

method istartswith(other: Any, escape: str | None = None, autoescape: bool = False) → ColumnOperators

继承自 ColumnOperators.istartswith() 方法的 ColumnOperators

实现 istartswith 操作符,例如,ColumnOperators.startswith() 的不区分大小写版本。

生成一个 LIKE 表达式,用于对字符串值的开头进行不区分大小写匹配:

lower(column) LIKE lower(<other>) || '%'

例如:

stmt = select(sometable).\
    where(sometable.c.column.istartswith("foobar"))

由于该运算符使用LIKE,在表达式中存在的通配符字符"%""_"也将像通配符一样起作用。对于文字字符串值,可以将ColumnOperators.istartswith.autoescape标志设置为True,以对字符串值中这些字符的出现应用转义,使它们匹配为自身而不是通配符字符。或者,ColumnOperators.istartswith.escape参数将建立一个给定字符作为转义字符,当目标表达式不是文字字符串时可能会有用。

参数:

  • other – 待比较的表达式。通常是一个简单的字符串值,但也可以是任意的 SQL 表达式。除非设置ColumnOperators.istartswith.autoescape标志为 True,否则不会默认转义 LIKE 通配符%_
  • autoescape
    布尔值;当为 True 时,在 LIKE 表达式中建立一个转义字符,然后将其应用于比较值中的所有"%""_"和转义字符本身的出现,假定比较值是一个文字字符串而不是一个 SQL 表达式。
    一个表达式,例如:
somecolumn.istartswith("foo%bar", autoescape=True)
  • 将呈现为:
lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '/'
  • 使用值:param"foo/%bar"
  • escape
    一个字符,当给定时,将使用ESCAPE关键字来将该字符作为转义字符。然后可以将该字符放在%_之前,以允许它们作为自身而不是通配符字符。
    例如:
somecolumn.istartswith("foo/%bar", escape="^")
  • 将呈现为:
lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '^'
  • 该参数也可以与ColumnOperators.istartswith.autoescape结合使用:
somecolumn.istartswith("foo%bar^bat", escape="^", autoescape=True)
  • 在上述情况下,给定的文字参数在传递到数据库之前将被转换为"foo^%bar^^bat"

另请参阅

ColumnOperators.startswith()

method like(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.like() 方法的 ColumnOperators

实现like运算符。

在列上下文中,生成表达式:

a LIKE other

例如:

stmt = select(sometable).\
    where(sometable.c.column.like("%foobar%"))

参数:

  • other – 待比较的表达式
  • escape
    可选的转义字符,渲染ESCAPE关键字,例如:
somecolumn.like("foo/%bar", escape="/")

另请参阅

ColumnOperators.ilike()

attribute local_attr

AssociationProxyInstance引用的‘local’类属性。

另请参阅

AssociationProxyInstance.attr

AssociationProxyInstance.remote_attr

method match(other: Any, **kwargs: Any) → ColumnOperators

继承自 ColumnOperators.match() 方法的 ColumnOperators

实现特定于数据库的‘match’操作符。

ColumnOperators.match()尝试解析为后端提供的类似 MATCH 的函数或操作符。例如:

  • PostgreSQL - 渲染x @@ plainto_tsquery(y)

从版本 2.0 开始更改:现在在 PostgreSQL 中使用plainto_tsquery()代替to_tsquery();为了与其他形式兼容,请参阅全文搜索。

  • MySQL - 渲染MATCH (x) AGAINST (y IN BOOLEAN MODE)
    另请参阅
    match - 具有附加功能的 MySQL 特定构造。
  • Oracle - 渲染CONTAINS(x, y)
  • 其他后端可能提供��殊实现。
  • 没有任何特殊实现的后端将将操作符发出为“MATCH”。例如,这与 SQLite 兼容。
method not_ilike(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.not_ilike() 方法的 ColumnOperators

实现NOT ILIKE操作符。

这等同于使用ColumnOperators.ilike()进行否定,即~x.ilike(y)

从版本 1.4 开始更改:not_ilike()操作符从先前版本的notilike()重命名。以前的名称仍可用于向后兼容。

另请参阅

ColumnOperators.ilike()

method not_in(other: Any) → ColumnOperators

继承自 ColumnOperators.not_in() 方法的 ColumnOperators

实现 NOT IN 运算符。

这相当于使用 ColumnOperators.in_() 的否定,即 ~x.in_(y)

other 是空序列的情况下,编译器会生成一个“空 not in” 表达式。 默认情况下,这会转换为表达式 “1 = 1”,以在所有情况下产生 true。 可以使用 create_engine.empty_in_strategy 来更改此行为。

在版本 1.4 中更改:not_in() 运算符从先前版本的 notin_() 重命名。 以前的名称仍可用于向后兼容。

在版本 1.2 中更改:ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认情况下会为空的 IN 序列生成一个“静态” 表达式。

另请参阅

ColumnOperators.in_()

method not_like(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.not_like() 方法的 ColumnOperators

实现 NOT LIKE 运算符。

这相当于使用 ColumnOperators.like() 的否定,即 ~x.like(y)

在���本 1.4 中更改:not_like() 运算符从先前版本的 notlike() 重命名。 以前的名称仍可用于向后兼容。

另请参阅

ColumnOperators.like()

method notilike(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.notilike() 方法的 ColumnOperators

实现 NOT ILIKE 运算符。

这相当于使用ColumnOperators.ilike()时进行否定,即~x.ilike(y)

从 1.4 版本开始更改:not_ilike()操作符从先前版本的notilike()重命名。以前的名称仍可用于向后兼容。

另请参阅

ColumnOperators.ilike()

method notin_(other: Any) → ColumnOperators

继承自 ColumnOperators.notin_() 的方法 ColumnOperators

实现NOT IN操作符。

这相当于使用ColumnOperators.in_()时进行否定,即~x.in_(y)

如果other是一个空序列,则编译器会生成一个“空 not in”表达式。默认情况下,这将产生“1 = 1”表达式,以在所有情况下产生 true。create_engine.empty_in_strategy可用于更改此行为。

从 1.4 版本开始更改:not_in()操作符从先前版本的notin_()重命名。以前的名称仍可用于向后兼容。

从 1.2 版本开始更改:ColumnOperators.in_()ColumnOperators.not_in()操作符现在默认情况下为一个空 IN 序列生成“静态”表达式。

另请参阅

ColumnOperators.in_()

method notlike(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.notlike() 的方法 ColumnOperators

实现NOT LIKE操作符。

这相当于使用ColumnOperators.like()时进行否定,即~x.like(y)

从 1.4 版本开始更改:not_like()操作符从先前版本的notlike()重命名。以前的名称仍可用于向后兼容。

另请参阅

ColumnOperators.like()

method nulls_first() → ColumnOperators

继承自 ColumnOperators.nulls_first() 方法 ColumnOperators

生成针对父对象的 nulls_first() 子句。

从版本 1.4 开始:nulls_first() 操作符从之前的版本 nullsfirst() 重新命名。以前的名称仍然可用于向后兼容。

method nulls_last() → ColumnOperators

继承自 ColumnOperators.nulls_last() 方法 ColumnOperators

生成针对父对象的 nulls_last() 子句。

从版本 1.4 开始:nulls_last() 操作符从之前的版本 nullslast() 重新命名。以前的名称仍然可用于向后兼容。

method nullsfirst() → ColumnOperators

继承自 ColumnOperators.nullsfirst() 方法 ColumnOperators

生成针对父对象的 nulls_first() 子句。

从版本 1.4 开始:nulls_first() 操作符从之前的版本 nullsfirst() 重新命名。以前的名称仍然可用于向后兼容。

method nullslast() → ColumnOperators

继承自 ColumnOperators.nullslast() 方法 ColumnOperators

生成针对父对象的 nulls_last() 子句。

从版本 1.4 开始:nulls_last() 操作符从之前的版本 nullslast() 重新命名。以前的名称仍然可用于向后兼容。

method op(opstring: str, precedence: int = 0, is_comparison: bool = False, return_type: Type[TypeEngine[Any]] | TypeEngine[Any] | None = None, python_impl: Callable[..., Any] | None = None) → Callable[[Any], Operators]

继承自 Operators.op() 方法的 Operators

生成一个通用的运算符函数。

例如:

somecolumn.op("*")(5)

产生:

somecolumn * 5

这个函数也可以用于使按位运算符明确。例如:

somecolumn.op('&')(0xff)

somecolumn中值的按位与。

参数:

  • opstring - 一个字符串,将作为此元素与传递给生成函数的表达式之间的中缀运算符输出。
  • precedence -
    数据库在 SQL 表达式中预期应用于运算符的优先级。这个整数值作为 SQL 编译器的提示,用于确定何时应该在特定操作周围添加显式括号。较低的数字将导致表达式在应用于具有较高优先级的其他运算符时被括在括号中。默认值为0,低于所有运算符,除了逗号(,)和AS运算符。值为 100 将高于或等于所有运算符,-100 将低于或等于所有运算符。
    另请参阅
    我正在使用 op() 生成自定义运算符,但我的括号没有正确显示 - SQLAlchemy SQL 编译器如何渲染括号的详细描述
  • is_comparison -
    传统;如果为 True,则该运算符将被视为“比较”运算符,即评估为布尔真/假值的运算符,如==>等。提供此标志是为了使 ORM 关系能够在自定义连接条件中确定该运算符是比较运算符。
    使用is_comparison参数已被使用Operators.bool_op()方法取代;这个更简洁的运算符会自动设置这个参数,但也提供了正确的PEP 484类型支持,因为返回的对象将表达“布尔”数据类型,即BinaryExpression[bool]
  • return_type - 一个TypeEngine类或对象,将强制此运算符生成的表达式的返回类型为该类型。默认情况下,指定Operators.op.is_comparison的运算符将解析为Boolean,而那些不指定的将与左操作数的类型相同。
  • python_impl -
    可选的  Python 函数,可以以与在数据库服务器上运行时此运算符的工作方式相同的方式评估两个 Python 值。适用于在 Python 中进行  SQL 表达式评估函数,例如用于 ORM 混合属性的函数,以及在多行更新或删除后用于匹配会话中的对象的 ORM “评估器”。
    例如:
>>> expr = column('x').op('+', python_impl=lambda a, b: a + b)('y')
  • 上述表达式的运算符也适用于非 SQL 左侧和右侧对象:
>>> expr.operator(5, 10)
15
  • 2.0 版新功能。

另请参阅

Operators.bool_op()

重新定义和创建新运算符

在连接条件中使用自定义运算符

method operate(op: OperatorType, *other: Any, **kwargs: Any) → Operators

继承自 Operators.operate() 方法的 Operators

对参数进行操作。

这是操作的最低级别,默认情况下引发 NotImplementedError

在子类上重新定义此操作可以使常见行为应用于所有操作。例如,覆盖 ColumnOperators 以将 func.lower() 应用于左侧和右侧:

class MyComparator(ColumnOperators):
    def operate(self, op, other, **kwargs):
        return op(func.lower(self), func.lower(other), **kwargs)

参数:

  • op – 运算符可调用。
  • *other – 操作的“其他”一侧。对于大多数操作,将是单个标量。
  • **kwargs – 修饰符。这些可能由特殊运算符传递,例如 ColumnOperators.contains()
attribute parent: _AssociationProxyProtocol[_T]
method regexp_match(pattern: Any, flags: str | None = None) → ColumnOperators

继承自 ColumnOperators.regexp_match() 方法的 ColumnOperators

实现特定于数据库的“regexp 匹配”运算符。

例如:

stmt = select(table.c.some_column).where(
    table.c.some_column.regexp_match('^(b|c)')
)

ColumnOperators.regexp_match() 尝试解析为后端提供的类似 REGEXP 的函数或运算符,但特定的正则表达式语法和可用标志不是后端不可知的

示例包括:

  • PostgreSQL - 渲染 x ~ yx !~ y(在否定时)。
  • Oracle - 渲染 REGEXP_LIKE(x, y)
  • SQLite - 使用 SQLite 的 REGEXP 占位符运算符,并调用 Python 的 re.match() 内置函数。
  • 其他后端可能提供特殊实现。
  • 没有任何特殊实现的后端将将运算符发出为 “REGEXP” 或 “NOT REGEXP”。例如,这与 SQLite 和 MySQL 兼容。

目前正实现正则表达式支持的数据库有 Oracle、PostgreSQL、MySQL 和 MariaDB。SQLite 可用部分支持。第三方方言中的支持可能有所不同。

参数:

  • pattern – 正则表达式模式字符串或列子句。
  • flags – 要应用的任何正则表达式字符串标志,仅作为普通 Python 字符串传递。这些标志是特定于后端的。某些后端,如 PostgreSQL 和  MariaDB,可能会将标志作为模式的一部分指定。在 PostgreSQL 中使用忽略大小写标志‘i’  时,将使用忽略大小写的正则表达式匹配运算符 ~*!~*

版本 1.4 中的新功能。

从版本 1.4.48 更改为 2.0.18:请注意,由于实现错误,“flags”参数先前接受 SQL  表达式对象,例如列表达式,而不仅仅是普通的 Python  字符串。这种实现与缓存不兼容,并已被移除;“flags”参数应仅传递字符串,因为这些标志将作为 SQL 表达式中的文字内联值呈现。

另请参阅

ColumnOperators.regexp_replace()

method regexp_replace(pattern: Any, replacement: Any, flags: str | None = None) → ColumnOperators

继承自 ColumnOperators.regexp_replace() 方法的 ColumnOperators

实现了特定于数据库的‘regexp replace’运算符。

例如:

stmt = select(
    table.c.some_column.regexp_replace(
        'b(..)',
        'XY',
        flags='g'
    )
)

ColumnOperators.regexp_replace() 试图解析为后端提供的类似 REGEXP_REPLACE 的函数,通常会生成函数 REGEXP_REPLACE()。然而,特定的正则表达式语法和可用的标志并非后端通用

目前正实现正则表达式替换支持的数据库有 Oracle、PostgreSQL、MySQL 8 或更高版本以及 MariaDB。第三方方言中的支持可能有所不同。

参数:

  • pattern – 正则表达式模式字符串或列子句。
  • pattern – 替换字符串或列子句。
  • flags – 要应用的任何正则表达式字符串标志,仅作为普通 Python 字符串传递。这些标志是特定于后端的。某些后端,如 PostgreSQL 和 MariaDB,可能会将标志作为模式的一部分指定。

版本 1.4 中的新功能。

在版本 1.4.48 更改为:2.0.18 请注意,由于实现错误,先前的“flags”参数接受了 SQL  表达式对象,例如列表达式,而不仅仅是普通的 Python  字符串。此实现与缓存一起使用时无法正常工作,并已删除;应该仅传递字符串作为“flags”参数,因为这些标志将作为 SQL  表达式中的文字内联值呈现。

另请参见

ColumnOperators.regexp_match()

attribute remote_attr

被此AssociationProxyInstance引用的“远程”类属性。

另请参见

AssociationProxyInstance.attr

AssociationProxyInstance.local_attr

method reverse_operate(op: OperatorType, other: Any, **kwargs: Any) → Operators

继承自 Operators.reverse_operate() 方法的 Operators

对参数进行反向操作。

用法与operate()相同。

attribute scalar

如果此AssociationProxyInstance代理本地端的标量关系,则返回True

method set(obj: Any, values: _T) → None
method startswith(other: Any, escape: str | None = None, autoescape: bool = False) → ColumnOperators

继承自 ColumnOperators.startswith() 方法的 ColumnOperators

实现startswith运算符。

生成一个 LIKE 表达式,用于测试字符串值开头的匹配:

column LIKE <other> || '%'

例如:

stmt = select(sometable).\
    where(sometable.c.column.startswith("foobar"))

由于该操作符使用 LIKE,因此存在于 表达式中的通配符字符 "%""_" 也会像通配符一样起作用。对于字面字符串值,可以将 ColumnOperators.startswith.autoescape 标志设置为 True,以对字符串值内出现的这些字符进行转义,使它们匹配为它们自身而不是通配符字符。或者,ColumnOperators.startswith.escape 参数将建立一个给定字符作为转义字符,当目标表达式不是字面字符串时,这可能会有用。

参数:

  • other – 要比较的表达式。通常是一个普通的字符串值,但也可以是任意的 SQL 表达式。LIKE 通配符字符 %_ 默认情况下不会被转义,除非设置了 ColumnOperators.startswith.autoescape 标志为 True。
  • autoescape
    布尔值;当为 True 时,在 LIKE 表达式中建立一个转义字符,然后将其应用于比较值内所有出现的 "%""_" 和转义字符本身,假定比较值是一个字面字符串而不是 SQL 表达式。
    诸如:
somecolumn.startswith("foo%bar", autoescape=True)
  • 将呈现为:
somecolumn LIKE :param || '%' ESCAPE '/'
  • 其中值为 :param,为 "foo/%bar"
  • escape
    一个字符,当给定时,将使用 ESCAPE 关键字来建立该字符作为转义字符。然后可以将该字符放在 %_ 的出现之前,以允许它们作为自身而不是通配符字符。
    诸如:
somecolumn.startswith("foo/%bar", escape="^"
  • 将呈现为:
somecolumn LIKE :param || '%' ESCAPE '^'
  • 参数也可以与 ColumnOperators.startswith.autoescape 结合使用:
somecolumn.startswith("foo%bar^bat", escape="^", autoescape=True)
  • 在上述情况下,给定的字面参数将在传递到数据库之前转换为 "foo^%bar^^bat"

另请参阅

ColumnOperators.endswith()

ColumnOperators.contains()

ColumnOperators.like()

attribute target_class: Type[Any]

AssociationProxyInstance处理的中间类。

拦截的追加/设置/赋值事件将导致生成此类的新实例。

attribute timetuple: Literal[None] = None

继承自 ColumnOperators.timetuple 属性的 ColumnOperators

Hack,允许在左侧比较日期时间对象。

class sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance

一个AssociationProxyInstance,其目标是一个对象。

成员

le(), lt(), all_(), any(), any_(),  asc(), attr, between(), bitwise_and(), bitwise_lshift(), bitwise_not(),  bitwise_or(), bitwise_rshift(), bitwise_xor(), bool_op(), collate(),  concat(), contains(), desc(), distinct(), endswith(), has(),  icontains(), iendswith(), ilike(), in_(), is_(), is_distinct_from(),  is_not(), is_not_distinct_from(), isnot(), isnot_distinct_from(),  istartswith(), like(), local_attr, match(), not_ilike(), not_in(),  not_like(), notilike(), notin_(), notlike(), nulls_first(),  nulls_last(), nullsfirst(), nullslast(), op(), operate(),  regexp_match(), regexp_replace(), remote_attr, reverse_operate(),  scalar, startswith(), target_class, timetuple

类签名

sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstancesqlalchemy.ext.associationproxy.AssociationProxyInstance

method __le__(other: Any) → ColumnOperators

继承自 ColumnOperators sqlalchemy.sql.expression.ColumnOperators.__le__ 方法

实现<=运算符。

在列上下文中,生成子句a <= b

method __lt__(other: Any) → ColumnOperators

继承自 ColumnOperators sqlalchemy.sql.expression.ColumnOperators.__lt__ 方法

实现<运算符。

在列上下文中,生成子句a < b

method all_() → ColumnOperators

继承自 ColumnOperators ColumnOperators.all_() 方法

生成针对父对象的all_()子句。

有关all_()的文档,请参阅示例。

注意

请务必不要将更新的ColumnOperators.all_()方法与此方法的旧版混淆,此方法的旧版是针对ARRAY特定的Comparator.all()方法,其使用不同的调用风格。

method any(criterion: _ColumnExpressionArgument[bool] | None = None, **kwargs: Any) → ColumnElement[bool]

继承自 AssociationProxyInstance AssociationProxyInstance.any() 方法

使用 EXISTS 生成代理的“any”表达式。

此表达式将是使用底层代理属性的Comparator.any()和/或Comparator.has()运算符的组合产品。

method any_() → ColumnOperators

继承自 ColumnOperators ColumnOperators.any_() 方法

对父对象生成一个any_()子句。

请参阅any_()的文档以获取示例。

注意

请确保不要混淆较新的ColumnOperators.any_()方法与这个方法的旧版,即专用于ARRAYComparator.any()方法,它使用不同的调用风格。

method asc() → ColumnOperators

继承自 ColumnOperators.asc() 方法的 ColumnOperators

对父对象生成一个asc()子句。

attribute attr

继承自 AssociationProxyInstance.attr 属性的 AssociationProxyInstance

返回一个元组(local_attr, remote_attr)

该属性最初旨在促进使用Query.join()方法一次性跨越两个关系的连接,但这使用了一个已弃用的调用风格。

要使用select.join()Query.join()与关联代理一起使用,当前的方法是分别利用AssociationProxyInstance.local_attrAssociationProxyInstance.remote_attr属性:

stmt = (
    select(Parent).
    join(Parent.proxied.local_attr).
    join(Parent.proxied.remote_attr)
)

未来的版本可能会为关联代理属性提供更简洁的连接模式。

另请参阅

AssociationProxyInstance.local_attr

AssociationProxyInstance.remote_attr

method between(cleft: Any, cright: Any, symmetric: bool = False) → ColumnOperators

继承自 ColumnOperators.between() 方法的 ColumnOperators

对父对象生成 between() 子句,给定下限和上限范围。

method bitwise_and(other: Any) → ColumnOperators

继承自 ColumnOperators.bitwise_and() 方法的 ColumnOperators

通过 & 运算符生成位与操作。

版本 2.0.2 中的新功能。

另请参阅

位运算符

method bitwise_lshift(other: Any) → ColumnOperators

继承自 ColumnOperators.bitwise_lshift() 方法的 ColumnOperators

通过 << 运算符生成位左移操作。

版本 2.0.2 中的新功能。

另请参阅

位运算符

method bitwise_not() → ColumnOperators

继承自 ColumnOperators.bitwise_not() 方法的 ColumnOperators

通过 ~ 运算符生成位非操作。

版本 2.0.2 中的新功能。

另请参阅

位运算符

method bitwise_or(other: Any) → ColumnOperators

继承自 ColumnOperators.bitwise_or() 方法的 ColumnOperators

通过 | 运算符生成位或操作。

版本 2.0.2 中的新功能。

另请参阅

位运算符

method bitwise_rshift(other: Any) → ColumnOperators

继承自 ColumnOperators.bitwise_rshift() 方法的 ColumnOperators

通过 >> 运算符生成位右移操作。

版本 2.0.2 中的新功能。

另请参阅

位运算符

method bitwise_xor(other: Any) → ColumnOperators

继承自 ColumnOperators.bitwise_xor() 方法的 ColumnOperators

生成一个按位异或操作,通常通过 ^ 运算符实现,或在 PostgreSQL 上使用 #

版本 2.0.2 中的新功能。

另请参阅

位运算符

method bool_op(opstring: str, precedence: int = 0, python_impl: Callable[[...], Any] | None = None) → Callable[[Any], Operators]

继承自 Operators.bool_op() 方法的 Operators

返回一个自定义布尔运算符。

这个方法是调用 Operators.op() 并传递 Operators.op.is_comparison 标志为 True 的简写。 使用 Operators.bool_op() 的一个关键优势是,在使用列构造时,返回表达式的“布尔”性质将出现在 PEP 484 目的中。

另请参阅

Operators.op()

method collate(collation: str) → ColumnOperators

继承自 ColumnOperators.collate() 方法的 ColumnOperators

生成一个针对父对象的 collate() 子句,给定排序规则字符串。

另请参阅

collate()

method concat(other: Any) → ColumnOperators

继承自 ColumnOperators.concat() 方法的 ColumnOperators

实现“concat”运算符。

在列上下文中,生成子句 a || b,或在 MySQL 上使用 concat() 运算符。

method contains(other: Any, **kw: Any) → ColumnElement[bool]

使用 EXISTS 生成一个代理的“包含”表达式。

这个表达式将使用底层代理属性的Comparator.any()Comparator.has()和/或Comparator.contains()操作符组成的产品。

method desc() → ColumnOperators

继承自 ColumnOperators.desc() 方法的 ColumnOperators

对父对象生成一个desc()子句。

method distinct() → ColumnOperators

继承自 ColumnOperators.distinct() 方法的 ColumnOperators

对父对象生成一个distinct()子句。

method endswith(other: Any, escape: str | None = None, autoescape: bool = False) → ColumnOperators

继承自 ColumnOperators.endswith() 方法的 ColumnOperators

实现‘endswith’操作符。

生成一个 LIKE 表达式,用于测试字符串值的结尾匹配:

column LIKE '%' || <other>

例如:

stmt = select(sometable).\
    where(sometable.c.column.endswith("foobar"))

由于该操作符使用 LIKE,存在于表达式中的通配符字符 "%""_" 也将像通配符一样起作用。对于字面字符串值,可以将ColumnOperators.endswith.autoescape标志设置为True,以对字符串值中这些字符的出现进行转义,使它们匹配为它们自身而不是通配符字符。或者,ColumnOperators.endswith.escape参数将建立一个给定字符作为转义字符,当目标表达式不是字面字符串时,这可能会有用。

参数:

  • other – 要比较的表达式。通常是一个普通的字符串值,但也可以是任意的 SQL 表达式。除非设置ColumnOperators.endswith.autoescape标志为 True,否则 LIKE 通配符字符 %_ 默认不会被转义。
  • autoescape
    布尔值;当为 True 时,在 LIKE 表达式中建立一个转义字符,然后将其应用于比较值中的所有"%""_"和转义字符本身的出现,假定比较值为文字字符串而不是 SQL 表达式。
    例如:
somecolumn.endswith("foo%bar", autoescape=True)
  • 将呈现为:
somecolumn LIKE '%' || :param ESCAPE '/'
  • 使用值:param"foo/%bar"
  • escape
    一个字符,当给定时,将使用ESCAPE关键字来建立该字符作为转义字符。然后可以将此字符放在%_之前,以允许它们作为自身而不是通配符字符。
    例如:
somecolumn.endswith("foo/%bar", escape="^")
  • 将呈现为:
somecolumn LIKE '%' || :param ESCAPE '^'
  • 参数也可以与ColumnOperators.endswith.autoescape结合使用:
somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)
  • 在上述情况下,给定的文字参数将在传递到数据库之前转换为"foo^%bar^^bat"

另请参阅

ColumnOperators.startswith()

ColumnOperators.contains()

ColumnOperators.like()

method has(criterion: _ColumnExpressionArgument[bool] | None = None, **kwargs: Any) → ColumnElement[bool]

继承自 AssociationProxyInstance.has() 方法的 AssociationProxyInstance

生成一个使用 EXISTS 的代理‘has’表达式。

此表达式将使用基础代理属性的Comparator.any()和/或Comparator.has()运算符的组合产品。

method icontains(other: Any, **kw: Any) → ColumnOperators

继承自 ColumnOperators.icontains() 方法的 ColumnOperators

实现icontains运算符,例如,ColumnOperators.contains()的不区分大小写版本。

生成一个 LIKE 表达式,用于对字符串值中间的不区分大小写匹配进行测试:

lower(column) LIKE '%' || lower(<other>) || '%'

例如:

stmt = select(sometable).\
    where(sometable.c.column.icontains("foobar"))

由于该操作符使用LIKE,因此在表达式中存在的"%""_"通配符字符也将像通配符一样起作用。对于文字字符串值,可以将ColumnOperators.icontains.autoescape标志设置为True,以对字符串值中这些字符的出现进行转义,使它们作为自身而不是通配符字符进行匹配。或者,ColumnOperators.icontains.escape参数将确定一个给定字符作为转义字符,当目标表达式不是文字字符串时可能会有用。

参数:

  • other – 要进行比较的表达式。通常是一个普通字符串值,但也可以是任意 SQL 表达式。默认情况下,LIKE通配符字符%_不会被转义,除非设置了ColumnOperators.icontains.autoescape标志为 True。
  • autoescape
    布尔值;当为 True 时,在LIKE表达式中建立一个转义字符,然后将其应用于比较值中所有的"%""_"和转义字符本身的出现次数,假定比较值是一个文字字符串而不是 SQL 表达式。
    例如表达式:
somecolumn.icontains("foo%bar", autoescape=True)
  • 将呈现为:
lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '/'
  • 其中:param的值为"foo/%bar"
  • escape
    一个字符,当给定时,将使用ESCAPE关键字来确定该字符作为转义字符。然后可以将该字符放在%_之前,以使它们作为自身而不是通配符字符。
    例如表达式:
somecolumn.icontains("foo/%bar", escape="^")
  • 将呈现为:
lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '^'
  • 该参数也可以与ColumnOperators.contains.autoescape结合使用:
somecolumn.icontains("foo%bar^bat", escape="^", autoescape=True)
  • 在上述情况下,给定的文字参数将在传递到数据库之前转换为"foo^%bar^^bat"

另请参见

ColumnOperators.contains()

method iendswith(other: Any, escape: str | None = None, autoescape: bool = False) → ColumnOperators

继承自 ColumnOperators.iendswith() 方法的 ColumnOperators

实现iendswith运算符,例如,对ColumnOperators.endswith()的不区分大小写版本。

生成一个 LIKE 表达式,用于对字符串值的不区分大小写匹配进行测试:

lower(column) LIKE '%' || lower(<other>)

例如:

stmt = select(sometable).\
    where(sometable.c.column.iendswith("foobar"))

由于运算符使用了LIKE,因此存在于表达式中的通配符字符"%""_"也将像通配符一样工作。对于字面字符串值,可以将ColumnOperators.iendswith.autoescape标志设置为True,以对字符串值中这些字符的出现应用转义,以使它们与自身匹配而不是通配符字符。或者,ColumnOperators.iendswith.escape参数将建立一个给定的字符作为转义字符,这在目标表达式不是字面字符串时可能有用。

参数:

  • other – 要比较的表达式。这通常是一个简单的字符串值,但也可以是任意的 SQL 表达式。LIKE 通配符字符%_默认情况下不会转义,除非将ColumnOperators.iendswith.autoescape标志设置为 True。
  • autoescape
    布尔值;当为 True 时,在 LIKE 表达式中建立一个转义字符,然后将其应用于比较值中的所有出现的"%""_"和转义字符本身,假定比较值是一个字面字符串而不是一个 SQL 表达式。
    一个表达式,例如:
somecolumn.iendswith("foo%bar", autoescape=True)
  • 将呈现为:
lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '/'
  • param的值为"foo/%bar"
  • escape
    一个字符,当给定时,将使用ESCAPE关键字来建立该字符作为转义字符。然后,可以将此字符放在%_之前,以允许它们作为自身而不是通配符字符。
    一个表达式,例如:
somecolumn.iendswith("foo/%bar", escape="^")
  • 将呈现为:
lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '^'
  • 参数也可以与ColumnOperators.iendswith.autoescape结合使用:
somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)
  • 在上述情况下,给定的字面参数将在传递到数据库之前被转换为"foo^%bar^^bat"

另见

ColumnOperators.endswith()

method ilike(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.ilike() 方法的 ColumnOperators

实现ilike运算符,例如不区分大小写的 LIKE。

在列上下文中,产生形式为:

lower(a) LIKE lower(other)

或者在支持 ILIKE 运算符的后端:

a ILIKE other

例如:

stmt = select(sometable).\
    where(sometable.c.column.ilike("%foobar%"))

参数:

  • other – 要比较的表达式
  • escape
    可选的转义字符,呈现ESCAPE关键字,例如:
somecolumn.ilike("foo/%bar", escape="/")

另请参阅

ColumnOperators.like()

method in_(other: Any) → ColumnOperators

继承自 ColumnOperators.in_() 方法的 ColumnOperators

实现in运算符。

在列上下文中,生成子句column IN

给定的参数other可以是:

  • 一个字面值列表,例如:
stmt.where(column.in_([1, 2, 3]))
  • 在这种调用形式中,项目列表被转换为与给定列表相同长度的一组绑定参数:
WHERE COL IN (?, ?, ?)
  • 如果比较是针对包含多个表达式的tuple_(),则可以提供元组列表:
from sqlalchemy import tuple_
stmt.where(tuple_(col1, col2).in_([(1, 10), (2, 20), (3, 30)]))
  • 一个空列表,例如:
stmt.where(column.in_([]))
  • 在这种调用形式中,表达式呈现一个“空集”表达式。这些表达式针对各个后端进行了定制,并且通常试图将空的 SELECT 语句作为子查询。例如在 SQLite 上,表达式是:
WHERE col IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)
  • 从版本 1.4 开始更改:在所有情况下,空的 IN 表达式现在都使用运行时生成的 SELECT 子查询。
  • 可以使用绑定参数,例如bindparam(),如果它包含bindparam.expanding标志:
stmt.where(column.in_(bindparam('value', expanding=True)))
  • 在这种调用形式中,表达式呈现一个特殊的非 SQL 占位符表达式,看起来像:
WHERE COL IN ([EXPANDING_value])
  • 此占位符表达式在语句执行时被拦截,以转换为前面所示的可变数量的绑定参数形式。如果执行语句如下:
connection.execute(stmt, {"value": [1, 2, 3]})
  • 数据库将传递一个绑定参数给每个值:
WHERE COL IN (?, ?, ?)
  • 从版本 1.2 开始:添加了“扩展”绑定参数
    如果传递了一个空列表,则会呈现一个特殊的“空列表”表达式,该表达式特定于正在使用的数据库。在 SQLite 上,这将是:
WHERE COL IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)
  • 从版本 1.3 开始:“扩展”绑定参数现在支持空列表
  • 一个select()构造,通常是一个相关的标量选择:
stmt.where(
    column.in_(
        select(othertable.c.y).
        where(table.c.x == othertable.c.x)
    )
)
  • 在这种调用形式中,ColumnOperators.in_() 渲染如下:
WHERE COL IN (SELECT othertable.y
FROM othertable WHERE othertable.x = table.x)

参数:

other – 一组字面常量,一个select()构造,或者一个包含 bindparam.expanding 标志设置为 True 的 bindparam() 构造。

method is_(other: Any) → ColumnOperators

继承自 ColumnOperators.is_() 方法的 ColumnOperators

实现IS运算符。

通常,当与None值进行比较时,IS会自动生成,该值解析为NULL。然而,在某些平台上,如果要与布尔值进行比较,则可能希望显式使用IS

另请参阅

ColumnOperators.is_not()

method is_distinct_from(other: Any) → ColumnOperators

继承自 ColumnOperators.is_distinct_from() 方法的 ColumnOperators

实现IS DISTINCT FROM运算符。

大多数平台上会渲染“a IS DISTINCT FROM b”;在某些平台上,例如 SQLite,可能会渲染“a IS NOT b”。

method is_not(other: Any) → ColumnOperators

继承自 ColumnOperators.is_not() 方法的 ColumnOperators

实现IS NOT运算符。

通常,当与None值进行比较时,IS NOT会自动生成,该值解析为NULL。然而,在某些平台上,如果要与布尔值进行比较,则可能希望显式使用IS NOT

从版本 1.4 开始更改:is_not()运算符从先前版本的isnot()中重命名。以前的名称仍可用于向后兼容。

另请参阅

ColumnOperators.is_()

method is_not_distinct_from(other: Any) → ColumnOperators

继承自 ColumnOperators.is_not_distinct_from() 方法的 ColumnOperators

实现IS NOT DISTINCT FROM运算符。

在大多数平台上呈现“a IS NOT DISTINCT FROM b”; 在某些平台上,如 SQLite 可能会呈现“a IS b”。

1.4 版更改:is_not_distinct_from() 运算符从之前的版本isnot_distinct_from() 重命名。以前的名称仍可用于向后兼容性。

method isnot(other: Any) → ColumnOperators

继承自 ColumnOperators.isnot() 方法的 ColumnOperators

实现 IS NOT 运算符。

通常,与None的值进行比较时会自动生成IS NOT,它解析为NULL。但是,如果在某些平台上与布尔值进行比较时,明确使用IS NOT可能是可取的。

1.4 版更改:is_not() 运算符从之前的版本isnot() 重命名。以前的名称仍可用于向后兼容性。

另请参阅

ColumnOperators.is_()

method isnot_distinct_from(other: Any) → ColumnOperators

继承自 ColumnOperators.isnot_distinct_from() 方法的 ColumnOperators

实现 IS NOT DISTINCT FROM 运算符。

在大多数平台上呈现“a IS NOT DISTINCT FROM b”; 在某些平台上,如 SQLite 可能会呈现“a IS b”。

1.4 版更改:is_not_distinct_from() 运算符从之前的版本isnot_distinct_from() 重命名。以前的名称仍可用于向后兼容性。

method istartswith(other: Any, escape: str | None = None, autoescape: bool = False) → ColumnOperators

继承自 ColumnOperators.istartswith() 方法的 ColumnOperators

实现 istartswith 运算符,例如ColumnOperators.startswith() 的不区分大小写版本。

生成一个 LIKE 表达式,用于对字符串值的起始部分进行不区分大小写的匹配测试:

lower(column) LIKE lower(<other>) || '%'

例如:

stmt = select(sometable).\
    where(sometable.c.column.istartswith("foobar"))

由于操作符使用 LIKE,存在于 表达式中的通配符 "%""_" 也会像通配符一样起作用。对于字面字符串值,可以将 ColumnOperators.istartswith.autoescape 标志设置为 True,以将这些字符的出现转义为字符串值内部的这些字符,使它们匹配为它们自身而不是通配符。或者,ColumnOperators.istartswith.escape 参数将建立一个给定字符作为转义字符,当目标表达式不是字面字符串时可能会有用。

参数:

  • other – 待比较的表达式。通常是一个普通的字符串值,但也可以是任意的 SQL 表达式。LIKE 通配符 %_ 默认情况下不会被转义,除非 ColumnOperators.istartswith.autoescape 标志被设置为 True。
  • autoescape
    布尔值;当为 True 时,在 LIKE 表达式中建立一个转义字符,然后将其应用于比较值中的所有 "%""_" 和转义字符本身的出现,假设比较值是一个字面字符串而不是 SQL 表达式。
    诸如:
somecolumn.istartswith("foo%bar", autoescape=True)
  • 将呈现为:
lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '/'
  • 其中,参数的值为 :param,为 "foo/%bar"
  • escape
    一个字符,当给定时,将以 ESCAPE 关键字渲染,以将该字符作为转义字符。然后可以将该字符放在 %_ 的前面,以允许它们作为自身而不是通配符字符。
    诸如:
somecolumn.istartswith("foo/%bar", escape="^")
  • 将呈现为:
lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '^'
  • 此参数也可以与ColumnOperators.istartswith.autoescape组合使用:
somecolumn.istartswith("foo%bar^bat", escape="^", autoescape=True)
  • 在上述情况下,给定的字面参数将在传递到数据库之前转换为 "foo^%bar^^bat"

另见

ColumnOperators.startswith()

method like(other: Any, escape: str | None = None) → ColumnOperators

ColumnOperators.like() 方法继承

实现 like 操作符。

在列上下文中,生成表达式:

a LIKE other

例如:

stmt = select(sometable).\
    where(sometable.c.column.like("%foobar%"))

参数:

  • other – 待比较的表达式
  • escape
    可选的转义字符,渲染 ESCAPE 关键字,例如:
somecolumn.like("foo/%bar", escape="/")

另请参阅

ColumnOperators.ilike()

attribute local_attr

继承自 AssociationProxyInstance.local_attr 属性的 AssociationProxyInstance

AssociationProxyInstance 引用的 ‘local’ 类属性。

另请参阅

AssociationProxyInstance.attr

AssociationProxyInstance.remote_attr

method match(other: Any, **kwargs: Any) → ColumnOperators

继承自 ColumnOperators.match() 方法的 ColumnOperators

实现了特定于数据库的 ‘match’ 运算符。

ColumnOperators.match() 尝试解析为后端提供的类似 MATCH 的函数或运算符。例如:

  • PostgreSQL - 渲染 x @@ plainto_tsquery(y)

从版本 2.0 开始更改:现在对于 PostgreSQL 使用 plainto_tsquery() 而不是 to_tsquery();为了与其他形式兼容,请参阅全文搜索。

  • MySQL - 渲染 MATCH (x) AGAINST (y IN BOOLEAN MODE)
    另请参阅
    match - 具有附加功能的 MySQL 特定构造。
  • Oracle - 渲染 CONTAINS(x, y)
  • 其他后端可能提供特殊实现。
  • 没有任何特殊实现的后端将将运算符输出为 “MATCH”。例如,这与 SQLite 兼容。
method not_ilike(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.not_ilike() 方法的 ColumnOperators

实现 NOT ILIKE 运算符。

这相当于使用否定与ColumnOperators.ilike(),即~x.ilike(y)

从版本 1.4 开始更改:not_ilike()操作符从先前版本的notilike()重新命名。先前的名称仍然可用于向后兼容。

另请参阅

ColumnOperators.ilike()

method not_in(other: Any) → ColumnOperators

继承自 ColumnOperators.not_in() 方法的 ColumnOperators

实现NOT IN操作符。

这相当于使用否定与ColumnOperators.in_(),即~x.in_(y)

other为空序列的情况下,编译器会产生一个“空的不包含”表达式。默认情况下,这相当于表达式“1 = 1”,以在所有情况下产生真值。可以使用create_engine.empty_in_strategy来更改此行为。

从版本 1.4 开始更改:not_in()操作符从先前版本的notin_()重新命名。先前的名称仍然可用于向后兼容。

从版本 1.2 开始更改:ColumnOperators.in_()ColumnOperators.not_in() 操作符现在默认情况下会为空 IN 序列生成“静态”表达式。

另请参阅

ColumnOperators.in_()

method not_like(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.not_like() 方法的 ColumnOperators

实现NOT LIKE操作符。

这相当于使用否定与ColumnOperators.like(),即~x.like(y)

从版本 1.4 开始更改:not_like()操作符从先前版本的notlike()重新命名。先前的名称仍然可用于向后兼容。

另请参阅

ColumnOperators.like()

method notilike(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.notilike() 方法的 ColumnOperators

实现NOT ILIKE运算符。

这相当于使用对ColumnOperators.ilike()进行否定,即 ~x.ilike(y)

从版本 1.4 起更改:not_ilike()运算符从之前的版本notilike()重命名。以确保向后兼容性,以前的名称仍然可用。

另请参阅

ColumnOperators.ilike()

method notin_(other: Any) → ColumnOperators

继承自 ColumnOperators.notin_() 方法的 ColumnOperators

实现NOT IN运算符。

这相当于使用对ColumnOperators.in_()进行否定,即 ~x.in_(y)

other为空序列的情况下,编译器会生成一个“空不包含”表达式。这默认为表达式“1 = 1”,以在所有情况下产生 true。create_engine.empty_in_strategy 可用于更改此行为。

从版本 1.4 起更改:not_in()运算符从之前的版本notin_()重命名。以确保向后兼容性,以前的名称仍然可用。

从版本 1.2 起更改:ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认情况下为一个“静态”表达式,用于空 IN 序列。

另请参阅

ColumnOperators.in_()

method notlike(other: Any, escape: str | None = None) → ColumnOperators

继承自 ColumnOperators.notlike() 方法 ColumnOperators

实现 NOT LIKE 操作符。

这相当于使用 ColumnOperators.like() 的否定,即 ~x.like(y)

从版本 1.4 开始:not_like() 操作符从以前的版本中的 notlike() 改名为 not_like()。以前的名称仍然可用于向后兼容。

另请参见

ColumnOperators.like()

method nulls_first() → ColumnOperators

继承自 ColumnOperators.nulls_first() 方法 ColumnOperators

生成针对父对象的 nulls_first() 子句。

从版本 1.4 开始:nulls_first() 操作符从以前的版本中的 nullsfirst() 改名为 nulls_first()。以前的名称仍然可用于向后兼容。

method nulls_last() → ColumnOperators

继承自 ColumnOperators.nulls_last() 方法 ColumnOperators

生成针对父对象的 nulls_last() 子句。

从版本 1.4 开始:nulls_last() 操作符从以前的版本中的 nullslast() 改名为 nulls_last()。以前的名称仍然可用于向后兼容。

method nullsfirst() → ColumnOperators

继承自 ColumnOperators.nullsfirst() 方法 ColumnOperators

生成针对父对象的 nulls_first() 子句。

从版本 1.4 开始:nulls_first() 操作符从以前的版本中的 nullsfirst() 改名为 nulls_first()。以前的名称仍然可用于向后兼容。

method nullslast() → ColumnOperators

继承自 ColumnOperators.nullslast() 方法的 ColumnOperators

生成一个针对父对象的nulls_last()子句。

从版本 1.4 开始更改:nulls_last()运算符从先前版本的nullslast()重命名。以前的名称仍可用于向后兼容。

method op(opstring: str, precedence: int = 0, is_comparison: bool = False, return_type: Type[TypeEngine[Any]] | TypeEngine[Any] | None = None, python_impl: Callable[..., Any] | None = None) → Callable[[Any], Operators]

继承自 Operators.op() 方法的 Operators

生成一个通用的运算符函数。

例如:

somecolumn.op("*")(5)

产生:

somecolumn * 5

这个函数也可以用来明确地表示位运算符。例如:

somecolumn.op('&')(0xff)

somecolumn中值的按位与。

参数:

  • opstring – 一个字符串,将作为中缀运算符输出在此元素和传递给生成函数的表达式之间。
  • precedence
    数据库在 SQL 表达式中期望应用于运算符的优先级。这个整数值作为一个提示,告诉 SQL 编译器何时应该在特定操作周围渲染显式的括号。较低的数字将导致表达式在应用于具有更高优先级的另一个运算符时被括号括起来。默认值为0,低于所有运算符,除了逗号(,)和AS运算符。值为 100 将高于或等于所有运算符,-100 将低于或等于所有运算符。
    另请参见
    我正在使用 op()生成自定义运算符,但我的括号没有正确显示 - SQLAlchemy SQL 编译器如何渲染括号的详细描述
  • is_comparison
    legacy; 如果为 True,则该运算符将被视为“比较”运算符,即评估为布尔值的运算符,如==>等。提供此标志是为了 ORM 关系可以在自定义连接条件中建立该运算符是比较运算符。
    使用is_comparison参数已被使用Operators.bool_op()方法取代;这个更简洁的运算符会自动设置这个参数,但也提供正确的PEP 484类型支持,因为返回的对象将表示“布尔”数据类型,即BinaryExpression[bool]
  • return_typeTypeEngine类或对象,它将强制此运算符生成的表达式的返回类型为该类型。默认情况下,指定Operators.op.is_comparison的运算符将解析为Boolean,而未指定的运算符将与左操作数具有相同的类型。
  • python_impl
    可选的 Python 函数,可以在与在数据库服务器上运行此运算符时相同的方式评估两个 Python 值。用于在 Python 中的 SQL 表达式评估函数,例如用于 ORM 混合属性的函数,以及用于在多行更新或删除后匹配会话中的对象的 ORM “评估器”。
    例如:
>>> expr = column('x').op('+', python_impl=lambda a, b: a + b)('y')
  • 上述表达式的运算符也适用于非 SQL 左右对象:
>>> expr.operator(5, 10)
15
  • 自版本 2.0 新增。

另请参阅

Operators.bool_op()

重新定义和创建新运算符

在连接条件中使用自定义运算符

method operate(op: OperatorType, *other: Any, **kwargs: Any) → Operators

Operators.operate() 方法继承自 Operators

对参数进行操作。

这是操作的最低级别,默认情况下引发NotImplementedError

在子类中覆盖此方法可以使通用行为应用于所有操作。例如,覆盖ColumnOperators以将func.lower()应用于左右两侧:

class MyComparator(ColumnOperators):
    def operate(self, op, other, **kwargs):
        return op(func.lower(self), func.lower(other), **kwargs)

参数:

  • op – 运算符可调用对象。
  • *other – 操作的“other”一侧。对于大多数操作,它将是一个单一标量。
  • **kwargs – 修饰符。这些可以由特殊运算符(如ColumnOperators.contains())传递。
method regexp_match(pattern: Any, flags: str | None = None) → ColumnOperators

ColumnOperators.regexp_match() 方法继承自 ColumnOperators

实现一个特定于数据库的“正则表达式匹配”运算符。

例如:

stmt = select(table.c.some_column).where(
    table.c.some_column.regexp_match('^(b|c)')
)

ColumnOperators.regexp_match() 尝试解析为后端提供的类似 REGEXP 的函数或运算符,但特定的正则表达式语法和可用标志 不是后端无关的

例如:

  • PostgreSQL - 当否定时渲染 x ~ yx !~ y
  • Oracle - 渲染 REGEXP_LIKE(x, y)
  • SQLite - 使用 SQLite 的 REGEXP 占位符运算符,并调用 Python 的 re.match() 内置函数。
  • 其他后端可能提供特殊实现。
  • 没有任何特殊实现的后端将发出运算符 “REGEXP” 或 “NOT REGEXP”。例如,这与 SQLite 和 MySQL 兼容。

正则表达式支持当前已为 Oracle、PostgreSQL、MySQL 和 MariaDB 实现。对于 SQLite,部分支持可用。第三方方言之间的支持可能会有所不同。

参数:

  • pattern – 正则表达式模式字符串或列子句。
  • flags – 要应用的任何正则表达式字符串标志,仅作为纯 Python 字符串传递。这些标志是后端特定的。某些后端,例如 PostgreSQL 和  MariaDB,可能会将标志作为模式的一部分来指定。在 PostgreSQL 中使用忽略大小写标志 ‘i’  时,将使用忽略大小写正则表达式匹配运算符 ~*!~*

新功能,版本 1.4。

从版本 1.4.48 更改为:2.0.18 请注意,由于实现错误,先前“flags”参数接受 SQL  表达式对象,例如列表达式,而不仅仅是纯 Python  字符串。这个实现与缓存一起使用时无法正常工作,并且已被删除;仅应传递字符串作为“flags”参数,因为这些标志在 SQL  表达式中被呈现为文字行内值。

另请参见

ColumnOperators.regexp_replace()

method regexp_replace(pattern: Any, replacement: Any, flags: str | None = None) → ColumnOperators

继承自 ColumnOperators.regexp_replace() 方法于 ColumnOperators

实现特定于数据库的 “regexp 替换” 运算符。

例如:

stmt = select(
    table.c.some_column.regexp_replace(
        'b(..)',
        'XY',
        flags='g'
    )
)

ColumnOperators.regexp_replace() 尝试解析为后端提供的类似 REGEXP_REPLACE 的函数,通常会发出函数 REGEXP_REPLACE()。但是,特定的正则表达式语法和可用标志 不是后端无关的

正则表达式替换支持当前已为 Oracle、PostgreSQL、MySQL 8 或更高版本和 MariaDB 实现。第三方方言之间的支持可能会有所不同。

参数:

  • pattern – 正则表达式模式字符串或列子句。
  • pattern – 替换字符串或列子句。
  • flags – 要应用的任何正则表达式字符串标志,仅作为普通 Python 字符串传递。这些标志是特定于后端的。某些后端,如 PostgreSQL 和 MariaDB,可能会将标志作为模式的一部分来指定。

版本 1.4 中的新功能。

在版本 1.4.48 更改为:2.0.18 请注意,由于实现错误,“flags”参数先前接受了 SQL  表达式对象,例如列表达式,除了普通的 Python  字符串。此实现与缓存不正确,已被移除;应仅传递字符串作为“flags”参数,因为这些标志将作为 SQL 表达式中的文字内联值呈现。

另请参阅

ColumnOperators.regexp_match()

attribute remote_attr

继承自 AssociationProxyInstance.remote_attr 属性的 AssociationProxyInstance

AssociationProxyInstance引用的“remote”类属性。

另请参阅

AssociationProxyInstance.attr

AssociationProxyInstance.local_attr

method reverse_operate(op: OperatorType, other: Any, **kwargs: Any) → Operators

继承自 Operators.reverse_operate() 方法的 Operators

对参数进行反向操作。

使用与operate()相同。

attribute scalar

继承自 AssociationProxyInstance.scalar 属性�� AssociationProxyInstance

如果此AssociationProxyInstance代理本地端的标量关系,则返回True

method startswith(other: Any, escape: str | None = None, autoescape: bool = False) → ColumnOperators

继承自 ColumnOperators.startswith() 方法的 ColumnOperators

实现startswith操作符。

生成一个 LIKE 表达式,用于测试字符串值的起始匹配:

column LIKE <other> || '%'

例如:

stmt = select(sometable).\
    where(sometable.c.column.startswith("foobar"))

由于该操作符使用LIKE,因此存在于表达式中的通配符字符"%""_"也将像通配符一样起作用。对于字面字符串值,可以将 ColumnOperators.startswith.autoescape 标志设置为True,以对字符串值内部这些字符的出现应用转义,使它们匹配为自身而不是通配符字符。或者,ColumnOperators.startswith.escape 参数将建立给定字符作为逃逸字符,当目标表达式不是字面字符串时,这可能会有用。


SqlAlchemy 2.0 中文文档(二十九)(3)https://developer.aliyun.com/article/1560466

相关文章
|
2天前
|
SQL 关系型数据库 数据库
SqlAlchemy 2.0 中文文档(二十九)(3)
SqlAlchemy 2.0 中文文档(二十九)
16 4
|
2天前
|
SQL 数据库 Python
SqlAlchemy 2.0 中文文档(二十六)(4)
SqlAlchemy 2.0 中文文档(二十六)
13 2
|
2天前
|
SQL 存储 关系型数据库
SqlAlchemy 2.0 中文文档(二十九)(1)
SqlAlchemy 2.0 中文文档(二十九)
16 4
|
2天前
|
SQL 关系型数据库 数据库
SqlAlchemy 2.0 中文文档(二十九)(4)
SqlAlchemy 2.0 中文文档(二十九)
15 4
|
2天前
|
SQL 前端开发 关系型数据库
SqlAlchemy 2.0 中文文档(二十七)(2)
SqlAlchemy 2.0 中文文档(二十七)
14 2
|
2天前
|
SQL 缓存 前端开发
SqlAlchemy 2.0 中文文档(二十七)(5)
SqlAlchemy 2.0 中文文档(二十七)
10 2
|
2天前
|
自然语言处理 数据库 Python
SqlAlchemy 2.0 中文文档(二十六)(2)
SqlAlchemy 2.0 中文文档(二十六)
12 2
|
2天前
|
SQL 缓存 数据库连接
SqlAlchemy 2.0 中文文档(二十六)(3)
SqlAlchemy 2.0 中文文档(二十六)
11 2
|
2天前
|
SQL 缓存 数据库连接
SqlAlchemy 2.0 中文文档(二十六)(1)
SqlAlchemy 2.0 中文文档(二十六)
12 2
|
2天前
|
SQL 关系型数据库 数据库
SqlAlchemy 2.0 中文文档(二十九)(5)
SqlAlchemy 2.0 中文文档(二十九)
11 1