关联代理


AssociationProxy 用于创建跨关系的 Target 属性的读/写视图。它实质上隐藏了两个端点之间 “middle” 属性的用法,可用于从相关对象的集合或标量关系中挑选字段。或者减少使用 association 对象模式的冗长程度。关联代理创造性地应用,允许构建几乎任何几何图形的复杂集合和字典视图,并使用标准的、透明配置的关系模式持久化到数据库中。


简化标量集合


考虑两个类 UserKeyword 之间的多对多映射。每个 User 可以有任意数量的 Keyword 对象,反之亦然(多对多模式在 多对多 中介绍)。下面的示例以相同的方式说明了这种模式,除了添加到 User 类中名为 User.keywords

from __future__ import annotations

from typing import Final
from typing import List

from sqlalchemy import Column
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.associationproxy import AssociationProxy


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "user"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(64))
    kw: Mapped[List[Keyword]] = relationship(secondary=lambda: user_keyword_table)

    def __init__(self, name: str):
        self.name = name

    # proxy the 'keyword' attribute from the 'kw' relationship
    keywords: AssociationProxy[List[str]] = association_proxy("kw", "keyword")


class Keyword(Base):
    __tablename__ = "keyword"
    id: Mapped[int] = mapped_column(primary_key=True)
    keyword: Mapped[str] = mapped_column(String(64))

    def __init__(self, keyword: str):
        self.keyword = keyword


user_keyword_table: Final[Table] = Table(
    "user_keyword",
    Base.metadata,
    Column("user_id", Integer, ForeignKey("user.id"), primary_key=True),
    Column("keyword_id", Integer, ForeignKey("keyword.id"), primary_key=True),
)


在上面的示例中,association_proxy() 应用于 User 类生成 kw 关系的“视图”,该关系公开与每个 Keyword 对象关联的 .keyword 的字符串值。当字符串添加到集合中时,它还会透明地创建新的 Keyword 对象:

>>> user = User("jek")
>>> user.keywords.append("cheese-inspector")
>>> user.keywords.append("snack-ninja")
>>> print(user.keywords)
['cheese-inspector', 'snack-ninja']


要了解此机制,请首先查看 UserKeyword,而不使用 .keywords 关联代理。通常,读取和作与 User 关联的 “keyword” 字符串集合需要从每个集合元素遍历到 .keyword 属性,这可能会很尴尬。 下面的示例说明了相同的 在不使用关联代理的情况下应用的一系列作:

>>> # identical operations without using the association proxy
>>> user = User("jek")
>>> user.kw.append(Keyword("cheese-inspector"))
>>> user.kw.append(Keyword("snack-ninja"))
>>> print([keyword.keyword for keyword in user.kw])
['cheese-inspector', 'snack-ninja']


association_proxy() 函数生成的 AssociationProxy 对象是 Python 描述符的实例,不被视为被 Mapper 以任何方式“映射”。因此,它始终在映射类的类定义中内联指示,无论使用的是 Declarative 映射还是 Imperative 映射。


代理通过对基础 mapped 属性或集合进行作来响应作来运行,并且通过代理所做的更改会立即在 mapped 属性中显现出来,反之亦然。基础属性仍可完全访问。


首次访问时,关联代理会对目标集合执行内省作,以便其行为正确对应。诸如本地代理属性是集合(通常是)还是标量引用,以及集合是否像集合、列表或字典等细节被考虑在内,以便代理的行为应该像底层集合或属性一样。


创建新值


当 list append() 事件(或 set add()、dictionary __setitem__() 或标量赋值事件)被关联代理拦截时,它会使用其构造函数实例化“中间”对象的新实例,并将给定值作为单个参数传递。在上面的示例中,像这样的作:

user.keywords.append("cheese-inspector")


由关联代理转换为作:

user.kw.append(Keyword("cheese-inspector"))


该示例在这里有效,因为我们为 Keyword 设计了构造函数 接受单个位置参数,keyword.对于单参数构造函数不可行的情况,可以使用 association_proxy.creator 自定义关联代理的创建行为 参数,该参数引用一个可调用对象(即 Python 函数),该函数将生成 给定 singular 参数的新对象实例。下面我们对此进行说明 通常使用 lambda:

class User(Base):
    ...

    # use Keyword(keyword=kw) on append() events
    keywords: AssociationProxy[List[str]] = association_proxy(
        "kw", "keyword", creator=lambda kw: Keyword(keyword=kw)
    )


creator 函数在基于列表或集合的集合中接受单个参数,或者接受标量属性。对于基于字典的集合,它接受两个参数,“key”和“value”。下面的 代理到基于字典的集合 中就是一个示例。


简化关联对象


“关联对象”模式是多对多关系的扩展形式,在 关联对象 中进行了介绍。关联代理可用于在常规使用期间避免 “关联对象” 妨碍。


假设上面的 user_keyword 表有额外的列,我们想显式映射这些列,但在大多数情况下,我们不需要直接访问这些属性。下面,我们演示了一个新的映射,它引入了 UserKeywordAssociation 类,该类映射到前面所示的 user_keyword 表。这个类special_key 添加了一个额外的列,我们偶尔想要访问这个值,但在通常情况下不是。我们在名为 关键字,这将弥合与user_keyword_associations的差距 将 User 集合添加到每个 UserKeywordAssociation

from __future__ import annotations

from typing import List
from typing import Optional

from sqlalchemy import ForeignKey
from sqlalchemy import String
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.associationproxy import AssociationProxy
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "user"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(64))

    user_keyword_associations: Mapped[List[UserKeywordAssociation]] = relationship(
        back_populates="user",
        cascade="all, delete-orphan",
    )

    # association proxy of "user_keyword_associations" collection
    # to "keyword" attribute
    keywords: AssociationProxy[List[Keyword]] = association_proxy(
        "user_keyword_associations",
        "keyword",
        creator=lambda keyword_obj: UserKeywordAssociation(keyword=keyword_obj),
    )

    def __init__(self, name: str):
        self.name = name


class UserKeywordAssociation(Base):
    __tablename__ = "user_keyword"
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True)
    keyword_id: Mapped[int] = mapped_column(ForeignKey("keyword.id"), primary_key=True)
    special_key: Mapped[Optional[str]] = mapped_column(String(50))

    user: Mapped[User] = relationship(back_populates="user_keyword_associations")

    keyword: Mapped[Keyword] = relationship()


class Keyword(Base):
    __tablename__ = "keyword"
    id: Mapped[int] = mapped_column(primary_key=True)
    keyword: Mapped[str] = mapped_column("keyword", String(64))

    def __init__(self, keyword: str):
        self.keyword = keyword

    def __repr__(self) -> str:
        return f"Keyword({self.keyword!r})"


通过上面的配置,我们可以对每个 User 对象的 .keywords 集合进行作,每个对象都公开一个 Keyword 集合 对象:

>>> user = User("log")
>>> for kw in (Keyword("new_from_blammo"), Keyword("its_big")):
...     user.keywords.append(kw)
>>> print(user.keywords)
[Keyword('new_from_blammo'), Keyword('its_big')]


此示例与前面 简化标量集合,其中关联代理公开字符串集合,而不是组合对象的集合。在这种情况下,每个 .keywords.append()作等效于:

>>> user.user_keyword_associations.append(
...     UserKeywordAssociation(keyword=Keyword("its_heavy"))
... )


UserKeywordAssociation 对象有两个属性,这两个属性都在关联代理的 append()作范围内填充;.keyword,它指的是 Keyword 对象和 .user,它引用 User 对象。首先填充 .keyword 属性,因为关联代理会生成一个新的 UserKeywordAssociation 对象以响应 .append() 作,将给定的 Keyword 实例分配给 .keyword 属性。然后,由于 UserKeywordAssociation 对象被附加到 User.user_keyword_associations 集合中,配置为 back_populatesUser.user_keyword_associations UserKeywordAssociation.user 属性在给定的 UserKeywordAssociation 实例上初始化,以引用父 User 接收 append作。 special_key 参数保留其默认值 None


对于我们确实希望 special_key 具有值的情况,我们显式创建 UserKeywordAssociation 对象。下面我们分配所有三个属性,其中在构造期间分配 .user 具有将新的 UserKeywordAssociation 附加到 User.user_keyword_associations 集合的效果(通过关系):

>>> UserKeywordAssociation(
...     keyword=Keyword("its_wood"), user=user, special_key="my special key"
... )


关联代理向我们返回由所有这些作表示的 Keyword 对象的集合:

>>> print(user.keywords)
[Keyword('new_from_blammo'), Keyword('its_big'), Keyword('its_heavy'), Keyword('its_wood')]


代理到基于字典的集合


关联代理也可以代理基于字典的集合。SQLAlchemy 映射通常使用 attribute_keyed_dict() 集合类型来 创建词典集合,以及 基于自定义字典的集合


关联代理在检测到基于字典的集合的使用情况时调整其行为。当新值添加到字典中时,关联代理通过将两个参数传递给 creating 函数而不是一个参数(key 和 value)来实例化中间对象。与往常一样,此创建函数默认为中间类的构造函数,并且可以使用 creator 参数进行自定义。


下面,我们修改 UserKeywordAssociation 示例,以便 User.user_keyword_associations 集合现在将使用字典进行映射,其中 UserKeywordAssociation.special_key 参数将用作字典的键。 我们还会应用一个创建器 参数添加到 User.keywords 代理中,以便在将新元素添加到字典时适当地分配这些值:

from __future__ import annotations
from typing import Dict

from sqlalchemy import ForeignKey
from sqlalchemy import String
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.associationproxy import AssociationProxy
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import attribute_keyed_dict


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "user"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(64))

    # user/user_keyword_associations relationship, mapping
    # user_keyword_associations with a dictionary against "special_key" as key.
    user_keyword_associations: Mapped[Dict[str, UserKeywordAssociation]] = relationship(
        back_populates="user",
        collection_class=attribute_keyed_dict("special_key"),
        cascade="all, delete-orphan",
    )
    # proxy to 'user_keyword_associations', instantiating
    # UserKeywordAssociation assigning the new key to 'special_key',
    # values to 'keyword'.
    keywords: AssociationProxy[Dict[str, Keyword]] = association_proxy(
        "user_keyword_associations",
        "keyword",
        creator=lambda k, v: UserKeywordAssociation(special_key=k, keyword=v),
    )

    def __init__(self, name: str):
        self.name = name


class UserKeywordAssociation(Base):
    __tablename__ = "user_keyword"
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True)
    keyword_id: Mapped[int] = mapped_column(ForeignKey("keyword.id"), primary_key=True)
    special_key: Mapped[str]

    user: Mapped[User] = relationship(
        back_populates="user_keyword_associations",
    )
    keyword: Mapped[Keyword] = relationship()


class Keyword(Base):
    __tablename__ = "keyword"
    id: Mapped[int] = mapped_column(primary_key=True)
    keyword: Mapped[str] = mapped_column(String(64))

    def __init__(self, keyword: str):
        self.keyword = keyword

    def __repr__(self) -> str:
        return f"Keyword({self.keyword!r})"


我们将 .keywords 集合演示为字典,将 UserKeywordAssociation.special_key value 添加到 Keyword 对象中:

>>> user = User("log")

>>> user.keywords["sk1"] = Keyword("kw1")
>>> user.keywords["sk2"] = Keyword("kw2")

>>> print(user.keywords)
{'sk1': Keyword('kw1'), 'sk2': Keyword('kw2')}


复合关联代理


鉴于我们之前从关系到标量属性的代理、跨关联对象代理以及代理字典的示例,我们可以将所有三种技术组合在一起,以得到 User 一个 keywords 字典,严格处理映射到 String 关键字special_key 的字符串值。UserKeywordAssociationKeyword 类完全隐藏。这是通过在 User 上构建一个关联代理来实现的,该代理引用 UserKeywordAssociation 上的关联代理:

from __future__ import annotations

from sqlalchemy import ForeignKey
from sqlalchemy import String
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.associationproxy import AssociationProxy
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import attribute_keyed_dict


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "user"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(64))

    user_keyword_associations: Mapped[Dict[str, UserKeywordAssociation]] = relationship(
        back_populates="user",
        collection_class=attribute_keyed_dict("special_key"),
        cascade="all, delete-orphan",
    )
    # the same 'user_keyword_associations'->'keyword' proxy as in
    # the basic dictionary example.
    keywords: AssociationProxy[Dict[str, str]] = association_proxy(
        "user_keyword_associations",
        "keyword",
        creator=lambda k, v: UserKeywordAssociation(special_key=k, keyword=v),
    )

    def __init__(self, name: str):
        self.name = name


class UserKeywordAssociation(Base):
    __tablename__ = "user_keyword"
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True)
    keyword_id: Mapped[int] = mapped_column(ForeignKey("keyword.id"), primary_key=True)
    special_key: Mapped[str] = mapped_column(String(64))
    user: Mapped[User] = relationship(
        back_populates="user_keyword_associations",
    )

    # the relationship to Keyword is now called
    # 'kw'
    kw: Mapped[Keyword] = relationship()

    # 'keyword' is changed to be a proxy to the
    # 'keyword' attribute of 'Keyword'
    keyword: AssociationProxy[Dict[str, str]] = association_proxy("kw", "keyword")


class Keyword(Base):
    __tablename__ = "keyword"
    id: Mapped[int] = mapped_column(primary_key=True)
    keyword: Mapped[str] = mapped_column(String(64))

    def __init__(self, keyword: str):
        self.keyword = keyword


User.keywords 现在是字符串到字符串的字典,其中 UserKeywordAssociationKeyword 对象是使用关联代理透明地为我们创建和删除的。在下面的示例中,我们说明了赋值运算符的用法,该运算符也由关联代理适当处理,以立即将字典值应用于集合:

>>> user = User("log")
>>> user.keywords = {"sk1": "kw1", "sk2": "kw2"}
>>> print(user.keywords)
{'sk1': 'kw1', 'sk2': 'kw2'}

>>> user.keywords["sk3"] = "kw3"
>>> del user.keywords["sk2"]
>>> print(user.keywords)
{'sk1': 'kw1', 'sk3': 'kw3'}

>>> # illustrate un-proxied usage
... print(user.user_keyword_associations["sk3"].kw)
<__main__.Keyword object at 0x12ceb90>


我们上面的示例需要注意的是,由于是为每个字典集作创建的 Keyword 对象,因此该示例无法保持 Keyword 对象在其字符串名称上的唯一性,这是 一个标记场景,例如这个场景。 对于此使用案例,配方 建议使用 UniqueObject 或类似的创建策略,它将对 Keyword 类的构造函数应用“先查找,然后创建”策略,以便在给定名称已存在时返回已存在的 Keyword


使用 Association Proxies 查询


AssociationProxy 具有简单的 SQL 构造功能,这些功能在类级别的工作方式与其他 ORM 映射属性类似,并提供主要基于 SQL EXISTS 关键字的基本过滤支持。


注意


关联代理扩展的主要目的是允许使用已加载的映射对象实例改进持久性和对象访问模式。类绑定查询功能的用途有限,不会取代在使用 JOIN、预先加载选项等构造 SQL 查询时引用底层属性的需要。


对于本节,假设一个类同时具有引用列的关联代理以及引用相关对象的关联代理,如以下示例映射所示:

from __future__ import annotations
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.associationproxy import association_proxy, AssociationProxy
from sqlalchemy.orm import DeclarativeBase, relationship
from sqlalchemy.orm.collections import attribute_keyed_dict
from sqlalchemy.orm.collections import Mapped


class Base(DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "user"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(64))

    user_keyword_associations: Mapped[UserKeywordAssociation] = relationship(
        cascade="all, delete-orphan",
    )

    # object-targeted association proxy
    keywords: AssociationProxy[List[Keyword]] = association_proxy(
        "user_keyword_associations",
        "keyword",
    )

    # column-targeted association proxy
    special_keys: AssociationProxy[List[str]] = association_proxy(
        "user_keyword_associations", "special_key"
    )


class UserKeywordAssociation(Base):
    __tablename__ = "user_keyword"
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), primary_key=True)
    keyword_id: Mapped[int] = mapped_column(ForeignKey("keyword.id"), primary_key=True)
    special_key: Mapped[str] = mapped_column(String(64))
    keyword: Mapped[Keyword] = relationship()


class Keyword(Base):
    __tablename__ = "keyword"
    id: Mapped[int] = mapped_column(primary_key=True)
    keyword: Mapped[str] = mapped_column(String(64))


生成的 SQL 采用针对 EXISTS SQL 运算符的相关子查询的形式,因此可以在 WHERE 子句中使用它,而无需对封闭查询进行其他修改。如果关联代理的直接目标是映射列表达式,则可以使用标准列运算符,这些运算符将嵌入到子查询中。例如,直相等运算符:

>>> print(session.scalars(select(User).where(User.special_keys == "jek")))
SELECT "user".id AS user_id, "user".name AS user_name FROM "user" WHERE EXISTS (SELECT 1 FROM user_keyword WHERE "user".id = user_keyword.user_id AND user_keyword.special_key = :special_key_1)


LIKE 运算符:

>>> print(session.scalars(select(User).where(User.special_keys.like("%jek"))))
SELECT "user".id AS user_id, "user".name AS user_name FROM "user" WHERE EXISTS (SELECT 1 FROM user_keyword WHERE "user".id = user_keyword.user_id AND user_keyword.special_key LIKE :special_key_1)


对于直接目标是相关对象或集合的关联代理, 或相关对象上的其他关联代理或属性,可以使用面向关系的运算符来代替,例如 PropComparator.has()PropComparator.any() 的 PropComparator.any() 中。User.keywords 属性实际上是两个链接在一起的关联代理,因此当使用此代理生成 SQL 短语时,我们会得到两个级别的 EXISTS 子查询:

>>> print(session.scalars(select(User).where(User.keywords.any(Keyword.keyword == "jek"))))
SELECT "user".id AS user_id, "user".name AS user_name FROM "user" WHERE EXISTS (SELECT 1 FROM user_keyword WHERE "user".id = user_keyword.user_id AND (EXISTS (SELECT 1 FROM keyword WHERE keyword.id = user_keyword.keyword_id AND keyword.keyword = :keyword_1)))


这不是最有效的 SQL 形式,因此,虽然关联代理可以方便地快速生成 WHERE 标准,但应检查 SQL 结果并将其“展开”为明确的 JOIN 标准以获得最佳使用效果,尤其是在将关联代理链接在一起时。


在 1.3 版本发生变更: 关联代理根据目标类型提供不同的查询模式。请参阅 AssociationProxy 现在为面向列的目标提供标准列运算符


级联标量删除


在 1.3 版本加入.


给定一个映射为:

from __future__ import annotations
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.associationproxy import association_proxy, AssociationProxy
from sqlalchemy.orm import DeclarativeBase, relationship
from sqlalchemy.orm.collections import attribute_keyed_dict
from sqlalchemy.orm.collections import Mapped


class Base(DeclarativeBase):
    pass


class A(Base):
    __tablename__ = "test_a"
    id: Mapped[int] = mapped_column(primary_key=True)
    ab: Mapped[AB] = relationship(uselist=False)
    b: AssociationProxy[B] = association_proxy(
        "ab", "b", creator=lambda b: AB(b=b), cascade_scalar_deletes=True
    )


class B(Base):
    __tablename__ = "test_b"
    id: Mapped[int] = mapped_column(primary_key=True)


class AB(Base):
    __tablename__ = "test_ab"
    a_id: Mapped[int] = mapped_column(ForeignKey(A.id), primary_key=True)
    b_id: Mapped[int] = mapped_column(ForeignKey(B.id), primary_key=True)

    b: Mapped[B] = relationship()


A.b 的赋值将生成一个 AB 对象:

a.b = B()


A.b 关联是标量,并且包括使用参数 AssociationProxy.cascade_scalar_deletes 。启用此参数后,设置 A.b 设置为 None 也会删除 A.ab

a.b = None
assert a.ab is None


AssociationProxy.cascade_scalar_deletes 如果未设置,则上面的关联对象 a.ab 将保留。


请注意,这不是基于集合的关联代理的行为;在这种情况下,当删除代理集合的成员时,始终会删除 intermediary association 对象。是否删除行取决于关系级联设置。


另请参阅


叶 栅


标量关系


下面的示例说明了如何在一对多关系的 many 端使用关联代理,以访问标量对象的属性:

from __future__ import annotations

from typing import List

from sqlalchemy import ForeignKey
from sqlalchemy import String
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.associationproxy import AssociationProxy
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship


class Base(DeclarativeBase):
    pass


class Recipe(Base):
    __tablename__ = "recipe"
    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(64))

    steps: Mapped[List[Step]] = relationship(back_populates="recipe")
    step_descriptions: AssociationProxy[List[str]] = association_proxy(
        "steps", "description"
    )


class Step(Base):
    __tablename__ = "step"
    id: Mapped[int] = mapped_column(primary_key=True)
    description: Mapped[str]
    recipe_id: Mapped[int] = mapped_column(ForeignKey("recipe.id"))
    recipe: Mapped[Recipe] = relationship(back_populates="steps")

    recipe_name: AssociationProxy[str] = association_proxy("recipe", "name")

    def __init__(self, description: str) -> None:
        self.description = description


my_snack = Recipe(
    name="afternoon snack",
    step_descriptions=[
        "slice bread",
        "spread peanut butted",
        "eat sandwich",
    ],
)


可以使用以下方法打印 my_snack 的步骤摘要:

>>> for i, step in enumerate(my_snack.steps, 1):
...     print(f"Step {i} of {step.recipe_name!r}: {step.description}")
Step 1 of 'afternoon snack': slice bread
Step 2 of 'afternoon snack': spread peanut butted
Step 3 of 'afternoon snack': eat sandwich


API 文档


对象名称

描述


association_proxy(target_collection, attr, *, [创建者, getset_factory, proxy_factory, proxy_bulk_set, info, cascade_scalar_deletes, create_on_none_assignment, init, repr, default, default_factory, compare, kw_only, hash])


返回一个 Python 属性,该属性实现 target 属性的视图,该视图引用 target 成员的属性。


关联代理


一个描述符,它提供对象属性的读/写视图。

AssociationProxyExtensionType


一个枚举。

AssociationProxyInstance


提供特定于类和对象的结果的每类对象。

ColumnAssociationProxyInstance


一个 AssociationProxyInstance,它以 database 列为目标。

ObjectAssociationProxyInstance


一个 AssociationProxyInstance,它以对象为目标。


函数 sqlalchemy.ext.associationproxy。association_proxytarget_collection: strattr str*, creator:_CreatorProtocolNone=无, getset_factory:_GetSetFactoryProtocolNone=无, proxy_factory:_ProxyFactoryProtocolNone=无, proxy_bulk_set:_ProxyBulkSetProtocolNone=无, info:_InfoTypeNone=无, cascade_scalar_deletes: bool = Falsecreate_on_none_assignment: bool = False, init:_NoArgbool=_NoArg.NO_ARG, repr:_NoArgbool=_NoArg.NO_ARG, default:AnyNone=_NoArg.NO_ARG, default_factory:_NoArgCallable[[],_T]=_NoArg.NO_ARG, compare:_NoArgbool=_NoArg.NO_ARG, kw_only:_NoArgbool=_NoArg.NO_ARG, hash:_NoArgboolNone=_NoArg.NO_ARG AssociationProxy[任何]


返回一个 Python 属性,该属性实现 target 属性的视图,该视图引用 target 成员的属性。


返回的值是 AssociationProxy 的实例。


实现一个 Python 属性,将关系表示为更简单值的集合或标量值。proxied 属性将模拟目标的集合类型(list、dict 或 set),或者在一对一关系的情况下,模拟简单的标量值。


参数

  • target_collection – 作为直接属性的名称 目标。 此属性通常由 relationship() 链接到目标集合,但也可以是多对一或非标量关系。


  • attr– 目标对象的实例上可用的关联实例或实例上的属性。


  • 创建者


    自选。


    定义将新项添加到代理集合时的自定义行为。


    默认情况下,向集合添加新项将触发目标对象的实例的构造,并将给定项作为位置参数传递给目标构造函数。对于这还不够的情况,association_proxy.creator 可以提供一个可调用对象,该对象将在 适当的方式。


    对于面向列表和集合的集合,将单个参数传递给可调用对象。对于面向字典的集合,将传递两个参数,分别对应于 key 和 value。


    association_proxy.creator 可调用对象也被调用用于标量(即多对一、一对一)关系。如果 target relationship 属性的当前值为 None,则可调用对象将用于构造新对象。如果对象值已存在,则给定的属性值将填充到该对象上。


    另请参阅


    创造新价值

  • cascade_scalar_deletes


    当 True 时,表示将代理值设置为 None 或通过 del 删除它,也应删除 source 对象。仅适用于标量属性。通常,删除代理目标不会删除代理源,因为此对象可能具有其他仍需保留的状态。


    在 1.3 版本加入.


    另请参阅


    级联标量删除 - 完整使用示例

  • create_on_none_assignment


    当 True 时,指示将代理值设置为 None 应使用 creator 创建源对象(如果源对象不存在)。仅适用于标量属性。这与 assocation_proxy.cascade_scalar_deletes .


    2.0.18 新版功能.


  • 初始化


    特定于声明性数据类映射,指定映射的属性是否应是 __init__() 的一部分 方法。


    2.0.0b4 版本的新Function。

  • repr


    特定于声明性数据类映射,指定此 AssociationProxy 建立的属性 应该是 DataClass 进程生成的 __repr__() 方法的一部分。


    2.0.0b4 版本的新Function。

  • default_factory


    特定于 Declarative Dataclass Mapping,指定将作为 __init__() 的一部分发生的默认值生成函数 方法。


    2.0.0b4 版本的新Function。


  • 比较


    特定于 Declarative Dataclass Mapping,指示此字段是否 在生成 __eq__()__ne__() 方法。


    2.0.0b4 版本的新Function。

  • kw_only


    特定于声明性数据类映射,指示在生成由数据类进程生成的 __init__() 方法时,是否应将此字段标记为仅关键字。


    2.0.0b4 版本的新Function。


  • 哈希


    特定于 Declarative Dataclass Mapping,控制在为映射的类生成 __hash__() 方法时是否包含此字段。


    在 2.0.36 版本加入.


  • info —— 可选,将被分配给 如果存在,则 AssociationProxy.info


以下附加参数涉及在 AssociationProxy 对象中注入自定义行为,仅供高级使用:


参数
  • getset_factory


    自选。代理属性访问由例程自动处理,这些例程根据此代理的 attr 参数获取和设置值。


    如果您想自定义此行为,则可以提供 getset_factory可调用对象,它生成一个 gettersetter 函数。调用工厂时有两个参数,即底层集合的抽象类型和此代理实例。


  • proxy_factory – 可选。要模拟的集合类型是通过探查目标集合来确定的。如果你的集合类型不能通过 duck 类型来确定,或者你想使用不同的集合实现,你可以提供一个工厂函数来生成这些集合。仅适用于非标量关系。


  • proxy_bulk_set – 可选,与 proxy_factory 一起使用。


sqlalchemy.ext.associationproxy 中。AssociationProxy(关联代理)¶


一个描述符,它提供对象属性的读/写视图。


类签名


sqlalchemy.ext.associationproxy.AssociationProxy sqlalchemy.orm.base.InspectionAttrInfo sqlalchemy.orm.base.ORMDescriptorsqlalchemy.orm._DCAttributeOptionssqlalchemy.ext.associationproxy._AssociationProxyProtocol


方法 sqlalchemy.ext.associationproxy.AssociationProxy. __init__target_collection: strattr str*, creator:_CreatorProtocolNone=无, getset_factory:_GetSetFactoryProtocolNone=无, proxy_factory:_ProxyFactoryProtocolNone=无, proxy_bulk_set:_ProxyBulkSetProtocolNone=无, info:_InfoTypeNone=无, cascade_scalar_deletes: bool = Falsecreate_on_none_assignment: bool = False, attribute_options:_AttributeOptionsNone=无


构造新的 AssociationProxy


AssociationProxy 对象通常使用 association_proxy() 构造函数构造。有关所有参数的描述,请参阅 association_proxy() 的描述。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. cascade_scalar_deletes: bool

属性 sqlalchemy.ext.associationproxy.AssociationProxy. create_on_none_assignment: bool

属性 sqlalchemy.ext.associationproxy.AssociationProxy. 创建者:_CreatorProtocolNone

属性 sqlalchemy.ext.associationproxy.AssociationProxy. extension_type: InspectionAttrExtensionType = 'ASSOCIATION_PROXY'


扩展类型(如果有)。默认为 NotExtension.NOT_EXTENSION


方法 sqlalchemy.ext.associationproxy.AssociationProxy. for_classclass_: Type[Any], obj:objectNone=None AssociationProxyInstance[_T]


返回特定映射类的本地内部状态。


例如,给定一个类 User

class User(Base):
    # ...

    keywords = association_proxy("kws", "keyword")


如果我们 Mapper.all_orm_descriptors,并且我们想要查看此代理的目标类,该类由 User 映射:

inspect(User).all_orm_descriptors["keywords"].for_class(User).target_class


这将返回特定于 User 类的 AssociationProxyInstance 实例。AssociationProxy object 与其父类保持不可知。


参数

  • class_ —— 我们要为其返回 state 的类。


  • obj —— 可选,如果属性引用多态目标,则需要一个类的实例,例如,我们必须查看实际目标对象的类型才能获得完整路径。


在 1.3 版本加入: - AssociationProxy 不再存储特定于特定父类的任何状态;状态现在存储在每个类的 AssociationProxyInstance 对象中。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. getset_factory:_GetSetFactoryProtocolNone

属性 sqlalchemy.ext.associationproxy.AssociationProxy. 信息


继承自InspectionAttrInfoInspectionAttrInfo.info属性


与对象关联的 Info 字典,允许用户定义的数据与此 InspectionAttr 相关联。


字典是在首次访问时生成的。 或者 它可以指定为 column_property()、relationship()复合() 功能。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_aliased_class = False


继承自 InspectionAttr.is_aliased_class InspectionAttr 的属性


如果此对象是 AliasedClass 的实例,则为 True。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_attribute = True


如果此对象是 Python 描述符,则为 True。


这可以指多种类型之一。 通常为 QueryableAttribute,它代表 MapperProperty 处理属性事件。但也可以是扩展类型,如 AssociationProxyhybrid_propertyInspectionAttr.extension_type 将引用标识特定子类型的常量。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_bundle = False


继承自InspectionAttrInspectionAttr.is_bundle属性


如果此对象是 Bundle 的实例,则为 True。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_clause_element = False


继承自 InspectionAttr.is_clause_element InspectionAttr 的属性


如果此对象是 ClauseElement 的 ClauseElement


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_instance = False


继承自InspectionAttrInspectionAttr.is_instance属性


如果此对象是 InstanceState 的实例,则为 True。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_mapper = False


继承自InspectionAttrInspectionAttr.is_mapper属性


如果此对象是 Mapper 的实例,则为 True。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_property = False


继承自InspectionAttrInspectionAttr.is_property属性


如果此对象是 MapperProperty 的实例,则为 True。


属性 sqlalchemy.ext.associationproxy.AssociationProxy. is_selectable = False


继承自InspectionAttrInspectionAttr.is_selectable属性


如果此对象是 可选


属性 sqlalchemy.ext.associationproxy.AssociationProxy. str

属性 sqlalchemy.ext.associationproxy.AssociationProxy. proxy_bulk_set:_ProxyBulkSetProtocolNone

属性 sqlalchemy.ext.associationproxy.AssociationProxy. proxy_factory:_ProxyFactoryProtocolNone

属性 sqlalchemy.ext.associationproxy.AssociationProxy. target_collection: str

属性 sqlalchemy.ext.associationproxy.AssociationProxy. value_attr: str

sqlalchemy.ext.associationproxy 中。AssociationProxyInstance(关联代理实例)¶


提供特定于类和对象的结果的每类对象。


当根据特定类或类的实例调用时,即当它用作常规 Python 描述符时,AssociationProxy 会使用它。


当将 AssociationProxy 作为普通 Python 描述符引用时,AssociationProxyInstance 是实际提供信息的对象。在正常情况下,它的存在是透明的:

>>> User.keywords.scalar
False


AssociationProxy 对象被 直接访问,以便获得对 AssociationProxyInstanceAssociationProxy.for_class() 方法:

proxy_state = inspect(User).all_orm_descriptors["keywords"].for_class(User)

# view if proxy object is scalar or not
>>> proxy_state.scalar
False


在 1.3 版本加入.


类签名


sqlalchemy.ext.associationproxy.AssociationProxyInstance sqlalchemy.orm.base.SQLORMOperations


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. __eq__other Any ColumnOperators


继承自 sqlalchemy.sql.expression.ColumnOperators.__eq__ ColumnOperators 的方法


实现 == 运算符。


在列上下文中,生成子句 a = b。如果目标是 None,则生成 IS NULL。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. __le__other Any ColumnOperators


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


实现 <= 运算符。


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


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. __lt__other Any ColumnOperators


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


实现 < 运算符。


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


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. __ne__other Any ColumnOperators


继承自 sqlalchemy.sql.expression.ColumnOperators.__ne__ ColumnOperators 的方法


实现 != 运算符。


在列上下文中,生成子句 a != b。如果目标是 None,则生成 IS NOT NULL。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. all_() ColumnOperators


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


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


注意


一定不要混淆较新的 ColumnOperators.all_() 方法替换为旧版 版本,则 Comparator.all() 方法,它使用不同的调用样式


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. anycriterion:_ColumnExpressionArgument[bool]None=None, **kwargs Any ColumnElement[bool]


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


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


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. any_ ColumnOperators


针对父对象生成 any_() 子句。


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


注意


一定不要混淆较新的 ColumnOperators.any_() 方法替换为旧版 版本,则 Comparator.any() 方法,它使用不同的调用样式


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. asc ColumnOperators


针对父对象生成 asc() 子句。


attribute sqlalchemy.ext.associationproxy.AssociationProxyInstance. attr 属性¶


返回 (local_attr, remote_attr) 的元组。


此属性最初是为了方便使用 Query.join() 方法同时跨两个关系进行连接,但是这会使用已弃用的调用样式。


要将 select.join()Query.join() 与 一个关联代理,目前的方法是使用 AssociationProxyInstance.local_attr AssociationProxyInstance.remote_attr attributes 中:

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


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


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. betweencleft 任意cright 任意对称 bool = False ColumnOperators


在给定下限和上限范围的情况下,针对父对象生成一个 between() 子句。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. bitwise_andother Any ColumnOperators


产生一个按位的 AND 运算,通常通过 & 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. bitwise_lshiftother Any ColumnOperators


产生按位 LSHIFT 运算,通常通过 << 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. bitwise_not() ColumnOperators


产生一个按位的 NOT 运算,通常通过 ~ 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. bitwise_orother Any ColumnOperators


生成按位 OR 运算,通常通过 | 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. bitwise_rshiftother Any ColumnOperators


生成按位 RSHIFT 运算,通常通过 >> 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. bitwise_xorother Any ColumnOperators


生成按位 XOR 运算,通常通过 ^ 运算符,或者 # 表示 PostgreSQL。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. bool_opopstring strprecedence int = 0, python_impl:Callable[[...],Any]None=None Callable[[any] 运算符]


继承自OperatorsOperators.bool_op() 方法


返回自定义布尔运算符。


此方法是调用 Operators.op() 并将 Operators.op.is_comparison flag 替换为 True。 使用 Operators.bool_op() 的主要优势 是,当使用列构造时, 返回的 expression 将用于 PEP 484 目的。


另请参阅


运算符.op()


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. collatecollation str ColumnOperators


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


另请参阅


collate()


attribute sqlalchemy.ext.associationproxy.AssociationProxyInstance. collection_class:Type[Any]无

method sqlalchemy.ext.associationproxy.AssociationProxyInstance. concatother Any ColumnOperators


实现 'concat' 运算符。


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


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. containsother Any**kw Any ColumnOperators


实现 'contains' 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值中间的匹配项进行测试:

column LIKE '%' || <other> || '%'


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.contains.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.contains.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.contains.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.contains("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE '%' || :param || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.contains("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE '%' || :param || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.contains.autoescape

    somecolumn.contains("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. deleteobj Any None(无)¶

方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. desc ColumnOperators


针对父对象生成 desc() 子句。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. distinct 列运算符


针对父对象生成 distinct() 子句。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. endswithotherAny escape:strNone=None, autoescape bool = False 列运算符


实现 'endswith' 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值末尾的匹配项进行测试:

column LIKE '%' || <other>


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.endswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.endswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.endswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.endswith("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE '%' || :param ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.endswith("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE '%' || :param ESCAPE '^'


    该参数也可以与 ColumnOperators.endswith.autoescape

    somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


类方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. for_proxyparent AssociationProxy[_T]owning_class: type[Any]parent_instance Any AssociationProxyInstance[_T]

方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. getobj Any→_TNoneAssociationProxyInstance[_T]

method sqlalchemy.ext.associationproxy.AssociationProxyInstance. hascriterion:_ColumnExpressionArgument[bool]None=None, **kwargs Any ColumnElement[bool]


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


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


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. icontainsother Any**kw Any ColumnOperators


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


生成一个 LIKE 表达式,该表达式针对字符串值中间的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.icontains.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.icontains.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.icontains.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.icontains("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.icontains("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.contains.autoescape

    somecolumn.icontains("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. iendswithotherAny escape:strNone=None, autoescape bool = False 列运算符


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


生成一个 LIKE 表达式,该表达式针对字符串值末尾的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.iendswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.iendswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.iendswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.iendswith("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.iendswith("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '^'


    该参数也可以与 ColumnOperators.iendswith.autoescape

    somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. ilikeotherAny escape:strNone=None 列运算符


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


在列上下文中,生成以下任一形式的表达式:

lower(a) LIKE lower(other)


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

a ILIKE other


例如:

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

参数

  • other – 要比较的表达式


  • 转义


    可选的转义字符,将 ESCAPE 关键字,例如:

    somecolumn.ilike("foo/%bar", escape="/")


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. in_other Any ColumnOperators


实现 in 运算符。


在列上下文中,生成子句 IN <other>


给定的参数 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_([]))


    在此调用形式中,表达式呈现 “empty set” 表达式。这些表达式是针对各个后端量身定制的,通常尝试获取空的 SELECT 语句作为子查询。例如在 SQLite 上,表达式为:

    WHERE col IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)


    在 1.4 版本发生变更: 空的 IN 表达式现在在所有情况下都使用执行时生成的 SELECT 子查询。


  • 如果绑定参数包含 bindparam.expanding 标志,则可以使用该参数,例如 bindparam():

    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 版本加入: 添加了 “expanding” 绑定参数


    如果传递了空列表,则会呈现特定于正在使用的数据库的特殊“空列表”表达式。在 SQLite 上,这将是:

    WHERE COL IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)


    在 1.3 版本加入: “expanding” 绑定参数现在支持空列表


  • select() 结构,通常是相关的标量 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。


属性 sqlalchemy.ext.associationproxy.AssociationProxyInstance. 信息

方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. is_other Any ColumnOperators


实现 IS 运算符。


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


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. is_distinct_fromother Any ColumnOperators


实现 IS DISTINCT FROM 运算符。


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


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. is_notother Any ColumnOperators


实现 IS NOT 运算符。


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


在 1.4 版本发生变更: is_not() 运算符已从 isnot() 的 API 中。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. is_not_distinct_fromother Any ColumnOperators


实现 IS NOT DISTINCT FROM 运算符。


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


在 1.4 版本发生变更: is_not_distinct_from() 运算符已从早期版本中的 isnot_distinct_from() 重命名。以前的名称仍然可用于向后兼容。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. isnotother Any 列运算符


实现 IS NOT 运算符。


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


在 1.4 版本发生变更: is_not() 运算符已从 isnot() 的 API 中。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. isnot_distinct_fromother Any ColumnOperators


实现 IS NOT DISTINCT FROM 运算符。


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


在 1.4 版本发生变更: is_not_distinct_from() 运算符已从早期版本中的 isnot_distinct_from() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. istartswithotherAny escape:strNone=None, autoescape bool = False 列运算符


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


生成一个 LIKE 表达式,该表达式针对字符串值开头的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.istartswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.istartswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.istartswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.istartswith("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.istartswith("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.istartswith.autoescape

    somecolumn.istartswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. likeother Any, escape:strNone=None 列运算符


实现 like 运算符。


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

a LIKE other


例如:

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

参数

  • other – 要比较的表达式


  • 转义


    可选的转义字符,将 ESCAPE 关键字,例如:

    somecolumn.like("foo/%bar", escape="/")


属性 sqlalchemy.ext.associationproxy.AssociationProxyInstance. local_attr


此引用的 'local' 类属性 AssociationProxyInstance


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. matchother Any**kwargs Any 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 兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. not_ilikeotherAny escape:strNone=None ColumnOperators


实现 NOT ILIKE 运算符。


这相当于使用 ColumnOperators.ilike()的 ColumnOperators.ilike()中,即 ~x.ilike(y)。


在 1.4 版本发生变更: not_ilike() 运算符已从 notilike() 的 intent 函数。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. not_inother Any ColumnOperators


实现 NOT IN 运算符。


这相当于使用 ColumnOperators.in_(),~x.in_(y)。


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


在 1.4 版本发生变更: not_in() 运算符从 notin_() 中。以前的名称仍然可用于向后兼容。


在 1.2 版本发生变更: ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认为空 IN 序列生成 “static” 表达式。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. not_likeother Any, escape:strNone=None ColumnOperators


实现 NOT LIKE 运算符。


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


在 1.4 版本发生变更: not_like() 运算符已从 notlike() 的以前的名称仍然可用于向后兼容。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. notilikeother Any, escape:strNone=None 列运算符


实现 NOT ILIKE 运算符。


这相当于使用 ColumnOperators.ilike()的 ColumnOperators.ilike()中,即 ~x.ilike(y)。


在 1.4 版本发生变更: not_ilike() 运算符已从 notilike() 的 intent 函数。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. notin_other Any ColumnOperators


实现 NOT IN 运算符。


这相当于使用 ColumnOperators.in_(),~x.in_(y)。


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


在 1.4 版本发生变更: not_in() 运算符从 notin_() 中。以前的名称仍然可用于向后兼容。


在 1.2 版本发生变更: ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认为空 IN 序列生成 “static” 表达式。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. notlikeother Any, escape:strNone=None 列运算符


实现 NOT LIKE 运算符。


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


在 1.4 版本发生变更: not_like() 运算符已从 notlike() 的以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. nulls_first ColumnOperators


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


在 1.4 版本发生变更: nulls_first() 运算符已从以前版本中的 nullsfirst() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. nulls_last ColumnOperators


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


在 1.4 版本发生变更: nulls_last() 运算符已从以前版本中的 nullslast() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. nullsfirst ColumnOperators


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


在 1.4 版本发生变更: nulls_first() 运算符已从以前版本中的 nullsfirst() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. nullslast ColumnOperators


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


在 1.4 版本发生变更: nulls_last() 运算符已从以前版本中的 nullslast() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. opopstring strprecedence int = 0is_comparison: bool = false, return_type:Type[TypeEngine[any]]TypeEngine[any]None=None, python_impl:Callable[...,Any]None=None) Callable[[any] operators []


继承自OperatorsOperators.op() 方法


生成泛型运算符函数。


例如:

somecolumn.op("*")(5)


生产:

somecolumn * 5


此函数还可用于使按位运算符显式。例如:

somecolumn.op("&")(0xFF)


somecolumn 中值的按位 AND。


参数

  • opstring – 一个字符串,将作为此元素和传递给生成函数的表达式之间的中缀运算符输出。


  • 优先级


    优先级,数据库应应用于 SQL 表达式中的运算符。此整数值充当 SQL 编译器的提示,以了解何时应在特定作周围呈现显式括号。较小的数字将导致表达式在应用于另一个优先级较高的运算符时被括起来。默认值 0 低于除逗号 () 和 AS 运算符之外的所有运算符。值 100 将高于或等于所有运算符,而 -100 将小于或等于所有运算符。


    另请参阅


    我正在使用 op() 生成自定义运算符,但我的括号没有正确显示 - SQLAlchemy SQL 编译器如何呈现括号的详细说明

  • is_comparison


    遗产;如果为 True,则该运算符将被视为 “比较” 运算符,即计算结果为布尔值 true/false 值,如 ==> 等。提供此标志是为了让 ORM 关系可以确定该运算符在自定义连接条件中使用时是比较运算符。


    使用 is_comparison 参数将被使用 Operators.bool_op() 方法;这个更简洁的运算符会自动设置此参数,但也提供正确的 PEP 484 类型支持,因为返回的对象将表示“布尔”数据类型,即 BinaryExpression[bool]。


  • return_type—— TypeEngine 类或对象,它将 force 此运算符生成的表达式的返回类型 属于该类型。 默认情况下,指定 Operators.op.is_comparison将解析为 Boolean 的 Boolean 和那些不 boolean 的 Boolean 将与左侧作数的类型相同。

  • python_impl


    一个可选的 Python 函数,该函数可以计算两个 Python 值,其工作方式与此运算符在数据库服务器上运行时的工作方式相同。对于 Python 内部 SQL 表达式评估函数(例如 ORM 混合属性)以及用于在多行更新或删除后匹配会话中的对象的 ORM“评估器”非常有用。


    例如:

    >>> expr = column("x").op("+", python_impl=lambda a, b: a + b)("y")


    上述表达式的运算符也适用于非 SQL 的 left 和 right 对象:

    >>> expr.operator(5, 10)
    15


    2.0 版的新Function。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. operateop OperatorType*other Any**kwargs Any 运营商


继承自OperatorsOperators.operate() 方法


对参数进行作。


这是最低级别的作,加注 NotImplementedError 的 MethodS 错误。


在子类上覆盖 this 可以允许将通用行为应用于所有作。例如,重写 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()))传递。


attribute sqlalchemy.ext.associationproxy.AssociationProxyInstance. parent _AssociationProxyProtocol[_T]

方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. regexp_matchpattern Any, flags:strNone=None ColumnOperators


实现特定于数据库的 'regexp match' 运算符。


例如:

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 Database、PostgreSQL、MySQL 和 MariaDB 实施正则表达式支持。SQLite 提供部分支持。第三方方言之间的支持可能会有所不同。


参数

  • pattern– 正则表达式模式字符串或列子句。


  • flags– 要应用的任何正则表达式字符串标志,仅作为纯 Python 字符串传递。这些标志是特定于后端的。一些后端,如 PostgreSQL 和 MariaDB,也可以将标志指定为模式的一部分。在 PostgreSQL 中使用忽略大小写标志 'i' 时,将使用忽略大小写正则表达式匹配运算符 ~*!~*


在 1.4 版本加入.


在 1.4.48 版本发生变更: 2.0.18 请注意,由于实现错误,除了纯 Python 字符串之外,“flags”参数以前还接受 SQL 表达式对象,例如列表达式。此实现无法正确使用缓存,因此已被删除;只应为 “flags” 参数传递字符串,因为这些标志在 SQL 表达式中呈现为文本内联值。


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. regexp_replacepattern Anyreplacement Any, flags:strNone=None ColumnOperators


实现特定于数据库的 'regexp replace' 运算符。


例如:

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


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


目前为 Oracle Database、PostgreSQL、MySQL 8 或更高版本以及 MariaDB 实施了正则表达式替换支持。第三方方言之间的支持可能会有所不同。


参数

  • pattern– 正则表达式模式字符串或列子句。


  • pattern– 替换字符串或 column 子句。


  • flags– 要应用的任何正则表达式字符串标志,仅作为纯 Python 字符串传递。这些标志是特定于后端的。一些后端,如 PostgreSQL 和 MariaDB,也可以将标志指定为模式的一部分。


在 1.4 版本加入.


在 1.4.48 版本发生变更: 2.0.18 请注意,由于实现错误,除了纯 Python 字符串之外,“flags”参数以前还接受 SQL 表达式对象,例如列表达式。此实现无法正确使用缓存,因此已被删除;只应为 “flags” 参数传递字符串,因为这些标志在 SQL 表达式中呈现为文本内联值。


属性 sqlalchemy.ext.associationproxy.AssociationProxyInstance. remote_attr


此引用的 'remote' 类属性 AssociationProxyInstance


方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. reverse_operateop OperatorTypeother Any**kwargs Any 运算符


Reverse 对参数进行作。


用法与 operate() 相同。


attribute sqlalchemy.ext.associationproxy.AssociationProxyInstance. 标量


如果此 AssociationProxyInstance 在本地端代理标量关系。


method sqlalchemy.ext.associationproxy.AssociationProxyInstance. setobj Anyvalues _T

方法 sqlalchemy.ext.associationproxy.AssociationProxyInstance. startswithotherAny escape:strNone=None, autoescape bool = False 列运算符


实现 startswith 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值开头的匹配项进行测试:

column LIKE <other> || '%'


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.startswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.startswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.startswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.startswith("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE :param || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.startswith("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE :param || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.startswith.autoescape

    somecolumn.startswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


属性 sqlalchemy.ext.associationproxy.AssociationProxyInstance. target_class: Type[Any]


由 this 处理的 intermediary 类 AssociationProxyInstance


拦截的 append/set/assignment 事件将导致生成此类的新实例。


属性 sqlalchemy.ext.associationproxy.AssociationProxyInstance. timetuple Literal[None] = None


Hack 允许在 LHS 上比较日期时间对象。


sqlalchemy.ext.associationproxy 中。ObjectAssociationProxyInstance(对象关联代理实例)¶


一个 AssociationProxyInstance,它以对象为目标。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. __le__other Any ColumnOperators


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


实现 <= 运算符。


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


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. __lt__other Any ColumnOperators


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


实现 < 运算符。


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


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. all_() ColumnOperators


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


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


注意


一定不要混淆较新的 ColumnOperators.all_() 方法替换为旧版 版本,则 Comparator.all() 方法,它使用不同的调用样式


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. anycriterion:_ColumnExpressionArgument[bool]None=None, **kwargs Any ColumnElement[bool]


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


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


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. any_ ColumnOperators


针对父对象生成 any_() 子句。


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


注意


一定不要混淆较新的 ColumnOperators.any_() 方法替换为旧版 版本,则 Comparator.any() 方法,它使用不同的调用样式


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. asc ColumnOperators


针对父对象生成 asc() 子句。


attribute sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. attr 属性¶


返回 (local_attr, remote_attr) 的元组。


此属性最初是为了方便使用 Query.join() 方法同时跨两个关系进行连接,但是这会使用已弃用的调用样式。


要将 select.join()Query.join() 与 一个关联代理,目前的方法是使用 AssociationProxyInstance.local_attr AssociationProxyInstance.remote_attr attributes 中:

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


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


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. betweencleft 任意cright 任意对称 bool = False ColumnOperators


在给定下限和上限范围的情况下,针对父对象生成一个 between() 子句。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. bitwise_andother Any ColumnOperators


产生一个按位的 AND 运算,通常通过 & 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. bitwise_lshiftother Any ColumnOperators


产生按位 LSHIFT 运算,通常通过 << 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. bitwise_not() ColumnOperators


产生一个按位的 NOT 运算,通常通过 ~ 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. bitwise_orother Any ColumnOperators


生成按位 OR 运算,通常通过 | 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. bitwise_rshiftother Any ColumnOperators


生成按位 RSHIFT 运算,通常通过 >> 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. bitwise_xorother Any ColumnOperators


生成按位 XOR 运算,通常通过 ^ 运算符,或者 # 表示 PostgreSQL。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. bool_opopstring strprecedence int = 0, python_impl:Callable[[...],Any]None=None Callable[[any] 运算符]


继承自OperatorsOperators.bool_op() 方法


返回自定义布尔运算符。


此方法是调用 Operators.op() 并将 Operators.op.is_comparison flag 替换为 True。 使用 Operators.bool_op() 的主要优势 是,当使用列构造时, 返回的 expression 将用于 PEP 484 目的。


另请参阅


运算符.op()


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. collatecollation str ColumnOperators


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


另请参阅


collate()


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. concatother Any ColumnOperators


实现 'concat' 运算符。


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


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. containsother Any**kw Any ColumnElement[bool]


使用 EXISTS 生成一个代理的 'contains' 表达式。


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


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. desc ColumnOperators


针对父对象生成 desc() 子句。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. distinct 列运算符


针对父对象生成 distinct() 子句。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. endswithotherAny escape:strNone=None, autoescape bool = False 列运算符


实现 'endswith' 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值末尾的匹配项进行测试:

column LIKE '%' || <other>


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.endswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.endswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.endswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.endswith("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE '%' || :param ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.endswith("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE '%' || :param ESCAPE '^'


    该参数也可以与 ColumnOperators.endswith.autoescape

    somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. hascriterion:_ColumnExpressionArgument[bool]None=None, **kwargs Any ColumnElement[bool]


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


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


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. icontainsother Any**kw Any ColumnOperators


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


生成一个 LIKE 表达式,该表达式针对字符串值中间的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.icontains.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.icontains.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.icontains.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.icontains("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.icontains("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.contains.autoescape

    somecolumn.icontains("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. iendswithotherAny escape:strNone=None, autoescape bool = False 列运算符


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


生成一个 LIKE 表达式,该表达式针对字符串值末尾的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.iendswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.iendswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.iendswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.iendswith("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.iendswith("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '^'


    该参数也可以与 ColumnOperators.iendswith.autoescape

    somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. ilikeotherAny escape:strNone=None 列运算符


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


在列上下文中,生成以下任一形式的表达式:

lower(a) LIKE lower(other)


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

a ILIKE other


例如:

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

参数

  • other – 要比较的表达式


  • 转义


    可选的转义字符,将 ESCAPE 关键字,例如:

    somecolumn.ilike("foo/%bar", escape="/")


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. in_other Any ColumnOperators


实现 in 运算符。


在列上下文中,生成子句 IN <other>


给定的参数 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_([]))


    在此调用形式中,表达式呈现 “empty set” 表达式。这些表达式是针对各个后端量身定制的,通常尝试获取空的 SELECT 语句作为子查询。例如在 SQLite 上,表达式为:

    WHERE col IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)


    在 1.4 版本发生变更: 空的 IN 表达式现在在所有情况下都使用执行时生成的 SELECT 子查询。


  • 如果绑定参数包含 bindparam.expanding 标志,则可以使用该参数,例如 bindparam():

    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 版本加入: 添加了 “expanding” 绑定参数


    如果传递了空列表,则会呈现特定于正在使用的数据库的特殊“空列表”表达式。在 SQLite 上,这将是:

    WHERE COL IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)


    在 1.3 版本加入: “expanding” 绑定参数现在支持空列表


  • select() 结构,通常是相关的标量 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。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. is_other Any ColumnOperators


实现 IS 运算符。


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


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. is_distinct_fromother Any ColumnOperators


实现 IS DISTINCT FROM 运算符。


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


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. is_notother Any ColumnOperators


实现 IS NOT 运算符。


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


在 1.4 版本发生变更: is_not() 运算符已从 isnot() 的 API 中。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. is_not_distinct_fromother Any ColumnOperators


实现 IS NOT DISTINCT FROM 运算符。


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


在 1.4 版本发生变更: is_not_distinct_from() 运算符已从早期版本中的 isnot_distinct_from() 重命名。以前的名称仍然可用于向后兼容。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. isnotother Any 列运算符


实现 IS NOT 运算符。


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


在 1.4 版本发生变更: is_not() 运算符已从 isnot() 的 API 中。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. isnot_distinct_fromother Any ColumnOperators


实现 IS NOT DISTINCT FROM 运算符。


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


在 1.4 版本发生变更: is_not_distinct_from() 运算符已从早期版本中的 isnot_distinct_from() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. istartswithotherAny escape:strNone=None, autoescape bool = False 列运算符


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


生成一个 LIKE 表达式,该表达式针对字符串值开头的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.istartswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.istartswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.istartswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.istartswith("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.istartswith("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.istartswith.autoescape

    somecolumn.istartswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. likeother Any, escape:strNone=None 列运算符


实现 like 运算符。


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

a LIKE other


例如:

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

参数

  • other – 要比较的表达式


  • 转义


    可选的转义字符,将 ESCAPE 关键字,例如:

    somecolumn.like("foo/%bar", escape="/")


属性 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. local_attr


此引用的 'local' 类属性 AssociationProxyInstance


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. matchother Any**kwargs Any 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 兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. not_ilikeotherAny escape:strNone=None ColumnOperators


实现 NOT ILIKE 运算符。


这相当于使用 ColumnOperators.ilike()的 ColumnOperators.ilike()中,即 ~x.ilike(y)。


在 1.4 版本发生变更: not_ilike() 运算符已从 notilike() 的 intent 函数。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. not_inother Any ColumnOperators


实现 NOT IN 运算符。


这相当于使用 ColumnOperators.in_(),~x.in_(y)。


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


在 1.4 版本发生变更: not_in() 运算符从 notin_() 中。以前的名称仍然可用于向后兼容。


在 1.2 版本发生变更: ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认为空 IN 序列生成 “static” 表达式。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. not_likeother Any, escape:strNone=None ColumnOperators


实现 NOT LIKE 运算符。


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


在 1.4 版本发生变更: not_like() 运算符已从 notlike() 的以前的名称仍然可用于向后兼容。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. notilikeother Any, escape:strNone=None 列运算符


实现 NOT ILIKE 运算符。


这相当于使用 ColumnOperators.ilike()的 ColumnOperators.ilike()中,即 ~x.ilike(y)。


在 1.4 版本发生变更: not_ilike() 运算符已从 notilike() 的 intent 函数。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. notin_other Any ColumnOperators


实现 NOT IN 运算符。


这相当于使用 ColumnOperators.in_(),~x.in_(y)。


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


在 1.4 版本发生变更: not_in() 运算符从 notin_() 中。以前的名称仍然可用于向后兼容。


在 1.2 版本发生变更: ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认为空 IN 序列生成 “static” 表达式。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. notlikeother Any, escape:strNone=None 列运算符


实现 NOT LIKE 运算符。


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


在 1.4 版本发生变更: not_like() 运算符已从 notlike() 的以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. nulls_first ColumnOperators


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


在 1.4 版本发生变更: nulls_first() 运算符已从以前版本中的 nullsfirst() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. nulls_last ColumnOperators


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


在 1.4 版本发生变更: nulls_last() 运算符已从以前版本中的 nullslast() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. nullsfirst ColumnOperators


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


在 1.4 版本发生变更: nulls_first() 运算符已从以前版本中的 nullsfirst() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. nullslast ColumnOperators


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


在 1.4 版本发生变更: nulls_last() 运算符已从以前版本中的 nullslast() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. opopstring strprecedence int = 0is_comparison: bool = false, return_type:Type[TypeEngine[any]]TypeEngine[any]None=None, python_impl:Callable[...,Any]None=None) Callable[[any] operators []


继承自OperatorsOperators.op() 方法


生成泛型运算符函数。


例如:

somecolumn.op("*")(5)


生产:

somecolumn * 5


此函数还可用于使按位运算符显式。例如:

somecolumn.op("&")(0xFF)


somecolumn 中值的按位 AND。


参数

  • opstring – 一个字符串,将作为此元素和传递给生成函数的表达式之间的中缀运算符输出。


  • 优先级


    优先级,数据库应应用于 SQL 表达式中的运算符。此整数值充当 SQL 编译器的提示,以了解何时应在特定作周围呈现显式括号。较小的数字将导致表达式在应用于另一个优先级较高的运算符时被括起来。默认值 0 低于除逗号 () 和 AS 运算符之外的所有运算符。值 100 将高于或等于所有运算符,而 -100 将小于或等于所有运算符。


    另请参阅


    我正在使用 op() 生成自定义运算符,但我的括号没有正确显示 - SQLAlchemy SQL 编译器如何呈现括号的详细说明

  • is_comparison


    遗产;如果为 True,则该运算符将被视为 “比较” 运算符,即计算结果为布尔值 true/false 值,如 ==> 等。提供此标志是为了让 ORM 关系可以确定该运算符在自定义连接条件中使用时是比较运算符。


    使用 is_comparison 参数将被使用 Operators.bool_op() 方法;这个更简洁的运算符会自动设置此参数,但也提供正确的 PEP 484 类型支持,因为返回的对象将表示“布尔”数据类型,即 BinaryExpression[bool]。


  • return_type—— TypeEngine 类或对象,它将 force 此运算符生成的表达式的返回类型 属于该类型。 默认情况下,指定 Operators.op.is_comparison将解析为 Boolean 的 Boolean 和那些不 boolean 的 Boolean 将与左侧作数的类型相同。

  • python_impl


    一个可选的 Python 函数,该函数可以计算两个 Python 值,其工作方式与此运算符在数据库服务器上运行时的工作方式相同。对于 Python 内部 SQL 表达式评估函数(例如 ORM 混合属性)以及用于在多行更新或删除后匹配会话中的对象的 ORM“评估器”非常有用。


    例如:

    >>> expr = column("x").op("+", python_impl=lambda a, b: a + b)("y")


    上述表达式的运算符也适用于非 SQL 的 left 和 right 对象:

    >>> expr.operator(5, 10)
    15


    2.0 版的新Function。


method sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. operateop OperatorType*other Any**kwargs Any 运营商


继承自OperatorsOperators.operate() 方法


对参数进行作。


这是最低级别的作,加注 NotImplementedError 的 MethodS 错误。


在子类上覆盖 this 可以允许将通用行为应用于所有作。例如,重写 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()))传递。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. regexp_matchpattern Any, flags:strNone=None ColumnOperators


实现特定于数据库的 'regexp match' 运算符。


例如:

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 Database、PostgreSQL、MySQL 和 MariaDB 实施正则表达式支持。SQLite 提供部分支持。第三方方言之间的支持可能会有所不同。


参数

  • pattern– 正则表达式模式字符串或列子句。


  • flags– 要应用的任何正则表达式字符串标志,仅作为纯 Python 字符串传递。这些标志是特定于后端的。一些后端,如 PostgreSQL 和 MariaDB,也可以将标志指定为模式的一部分。在 PostgreSQL 中使用忽略大小写标志 'i' 时,将使用忽略大小写正则表达式匹配运算符 ~*!~*


在 1.4 版本加入.


在 1.4.48 版本发生变更: 2.0.18 请注意,由于实现错误,除了纯 Python 字符串之外,“flags”参数以前还接受 SQL 表达式对象,例如列表达式。此实现无法正确使用缓存,因此已被删除;只应为 “flags” 参数传递字符串,因为这些标志在 SQL 表达式中呈现为文本内联值。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. regexp_replacepattern Anyreplacement Any, flags:strNone=None ColumnOperators


实现特定于数据库的 'regexp replace' 运算符。


例如:

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


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


目前为 Oracle Database、PostgreSQL、MySQL 8 或更高版本以及 MariaDB 实施了正则表达式替换支持。第三方方言之间的支持可能会有所不同。


参数

  • pattern– 正则表达式模式字符串或列子句。


  • pattern– 替换字符串或 column 子句。


  • flags– 要应用的任何正则表达式字符串标志,仅作为纯 Python 字符串传递。这些标志是特定于后端的。一些后端,如 PostgreSQL 和 MariaDB,也可以将标志指定为模式的一部分。


在 1.4 版本加入.


在 1.4.48 版本发生变更: 2.0.18 请注意,由于实现错误,除了纯 Python 字符串之外,“flags”参数以前还接受 SQL 表达式对象,例如列表达式。此实现无法正确使用缓存,因此已被删除;只应为 “flags” 参数传递字符串,因为这些标志在 SQL 表达式中呈现为文本内联值。


属性 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. remote_attr


此引用的 'remote' 类属性 AssociationProxyInstance


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. reverse_operateop OperatorTypeother Any**kwargs Any 运算符


Reverse 对参数进行作。


用法与 operate() 相同。


attribute sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. 标量


如果此 AssociationProxyInstance 在本地端代理标量关系。


方法 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. startswithotherAny escape:strNone=None, autoescape bool = False 列运算符


实现 startswith 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值开头的匹配项进行测试:

column LIKE <other> || '%'


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.startswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.startswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.startswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.startswith("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE :param || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.startswith("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE :param || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.startswith.autoescape

    somecolumn.startswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


属性 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. target_class: Type[Any]


由 this 处理的 intermediary 类 AssociationProxyInstance


拦截的 append/set/assignment 事件将导致生成此类的新实例。


属性 sqlalchemy.ext.associationproxy.ObjectAssociationProxyInstance. timetuple Literal[None] = None


Hack 允许在 LHS 上比较日期时间对象。


sqlalchemy.ext.associationproxy 中。ColumnAssociationProxyInstance


一个 AssociationProxyInstance,它以 database 列为目标。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. __le__other Any ColumnOperators


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


实现 <= 运算符。


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


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. __lt__other Any ColumnOperators


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


实现 < 运算符。


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


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. __ne__other Any ColumnOperators


继承自 sqlalchemy.sql.expression.ColumnOperators.__ne__ ColumnOperators 的方法


实现 != 运算符。


在列上下文中,生成子句 a != b。如果目标是 None,则生成 IS NOT NULL。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. all_() ColumnOperators


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


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


注意


一定不要混淆较新的 ColumnOperators.all_() 方法替换为旧版 版本,则 Comparator.all() 方法,它使用不同的调用样式


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. anycriterion:_ColumnExpressionArgument[bool]None=None, **kwargs Any ColumnElement[bool]


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


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


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. any_ ColumnOperators


针对父对象生成 any_() 子句。


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


注意


一定不要混淆较新的 ColumnOperators.any_() 方法替换为旧版 版本,则 Comparator.any() 方法,它使用不同的调用样式


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. asc ColumnOperators


针对父对象生成 asc() 子句。


attribute sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. attr 属性¶


返回 (local_attr, remote_attr) 的元组。


此属性最初是为了方便使用 Query.join() 方法同时跨两个关系进行连接,但是这会使用已弃用的调用样式。


要将 select.join()Query.join() 与 一个关联代理,目前的方法是使用 AssociationProxyInstance.local_attr AssociationProxyInstance.remote_attr attributes 中:

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


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


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. betweencleft 任意cright 任意对称 bool = False ColumnOperators


在给定下限和上限范围的情况下,针对父对象生成一个 between() 子句。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. bitwise_andother Any ColumnOperators


产生一个按位的 AND 运算,通常通过 & 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. bitwise_lshiftother Any ColumnOperators


产生按位 LSHIFT 运算,通常通过 << 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. bitwise_not() ColumnOperators


产生一个按位的 NOT 运算,通常通过 ~ 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. bitwise_orother Any ColumnOperators


生成按位 OR 运算,通常通过 | 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. bitwise_rshiftother Any ColumnOperators


生成按位 RSHIFT 运算,通常通过 >> 算子。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. bitwise_xorother Any ColumnOperators


生成按位 XOR 运算,通常通过 ^ 运算符,或者 # 表示 PostgreSQL。


2.0.2 新版功能.


另请参阅


按位运算符


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. bool_opopstring strprecedence int = 0, python_impl:Callable[[...],Any]None=None Callable[[any] 运算符]


继承自OperatorsOperators.bool_op() 方法


返回自定义布尔运算符。


此方法是调用 Operators.op() 并将 Operators.op.is_comparison flag 替换为 True。 使用 Operators.bool_op() 的主要优势 是,当使用列构造时, 返回的 expression 将用于 PEP 484 目的。


另请参阅


运算符.op()


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. collatecollation str ColumnOperators


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


另请参阅


collate()


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. concatother Any ColumnOperators


实现 'concat' 运算符。


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


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. containsother Any**kw Any ColumnOperators


实现 'contains' 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值中间的匹配项进行测试:

column LIKE '%' || <other> || '%'


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.contains.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.contains.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.contains.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.contains("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE '%' || :param || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.contains("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE '%' || :param || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.contains.autoescape

    somecolumn.contains("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. desc ColumnOperators


针对父对象生成 desc() 子句。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. distinct 列运算符


针对父对象生成 distinct() 子句。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. endswithotherAny escape:strNone=None, autoescape bool = False 列运算符


实现 'endswith' 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值末尾的匹配项进行测试:

column LIKE '%' || <other>


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.endswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.endswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.endswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.endswith("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE '%' || :param ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.endswith("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE '%' || :param ESCAPE '^'


    该参数也可以与 ColumnOperators.endswith.autoescape

    somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. hascriterion:_ColumnExpressionArgument[bool]None=None, **kwargs Any ColumnElement[bool]


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


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


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. icontainsother Any**kw Any ColumnOperators


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


生成一个 LIKE 表达式,该表达式针对字符串值中间的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.icontains.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.icontains.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.icontains.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.icontains("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.icontains("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.contains.autoescape

    somecolumn.icontains("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. iendswithotherAny escape:strNone=None, autoescape bool = False 列运算符


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


生成一个 LIKE 表达式,该表达式针对字符串值末尾的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.iendswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.iendswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.iendswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.iendswith("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.iendswith("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE '%' || lower(:param) ESCAPE '^'


    该参数也可以与 ColumnOperators.iendswith.autoescape

    somecolumn.endswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. ilikeotherAny escape:strNone=None 列运算符


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


在列上下文中,生成以下任一形式的表达式:

lower(a) LIKE lower(other)


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

a ILIKE other


例如:

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

参数

  • other – 要比较的表达式


  • 转义


    可选的转义字符,将 ESCAPE 关键字,例如:

    somecolumn.ilike("foo/%bar", escape="/")


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. in_other Any ColumnOperators


实现 in 运算符。


在列上下文中,生成子句 IN <other>


给定的参数 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_([]))


    在此调用形式中,表达式呈现 “empty set” 表达式。这些表达式是针对各个后端量身定制的,通常尝试获取空的 SELECT 语句作为子查询。例如在 SQLite 上,表达式为:

    WHERE col IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)


    在 1.4 版本发生变更: 空的 IN 表达式现在在所有情况下都使用执行时生成的 SELECT 子查询。


  • 如果绑定参数包含 bindparam.expanding 标志,则可以使用该参数,例如 bindparam():

    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 版本加入: 添加了 “expanding” 绑定参数


    如果传递了空列表,则会呈现特定于正在使用的数据库的特殊“空列表”表达式。在 SQLite 上,这将是:

    WHERE COL IN (SELECT 1 FROM (SELECT 1) WHERE 1!=1)


    在 1.3 版本加入: “expanding” 绑定参数现在支持空列表


  • select() 结构,通常是相关的标量 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。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. is_other Any ColumnOperators


实现 IS 运算符。


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


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. is_distinct_fromother Any ColumnOperators


实现 IS DISTINCT FROM 运算符。


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


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. is_notother Any ColumnOperators


实现 IS NOT 运算符。


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


在 1.4 版本发生变更: is_not() 运算符已从 isnot() 的 API 中。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. is_not_distinct_fromother Any ColumnOperators


实现 IS NOT DISTINCT FROM 运算符。


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


在 1.4 版本发生变更: is_not_distinct_from() 运算符已从早期版本中的 isnot_distinct_from() 重命名。以前的名称仍然可用于向后兼容。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. isnotother Any 列运算符


实现 IS NOT 运算符。


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


在 1.4 版本发生变更: is_not() 运算符已从 isnot() 的 API 中。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. isnot_distinct_fromother Any ColumnOperators


实现 IS NOT DISTINCT FROM 运算符。


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


在 1.4 版本发生变更: is_not_distinct_from() 运算符已从早期版本中的 isnot_distinct_from() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. istartswithotherAny escape:strNone=None, autoescape bool = False 列运算符


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


生成一个 LIKE 表达式,该表达式针对字符串值开头的不敏感匹配项进行测试:

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


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.istartswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.istartswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.istartswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.istartswith("foo%bar", autoescape=True)


    将渲染为:

    lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.istartswith("foo/%bar", escape="^")


    将渲染为:

    lower(somecolumn) LIKE lower(:param) || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.istartswith.autoescape

    somecolumn.istartswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. likeother Any, escape:strNone=None 列运算符


实现 like 运算符。


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

a LIKE other


例如:

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

参数

  • other – 要比较的表达式


  • 转义


    可选的转义字符,将 ESCAPE 关键字,例如:

    somecolumn.like("foo/%bar", escape="/")


属性 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. local_attr


此引用的 'local' 类属性 AssociationProxyInstance


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. matchother Any**kwargs Any 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 兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. not_ilikeotherAny escape:strNone=None ColumnOperators


实现 NOT ILIKE 运算符。


这相当于使用 ColumnOperators.ilike()的 ColumnOperators.ilike()中,即 ~x.ilike(y)。


在 1.4 版本发生变更: not_ilike() 运算符已从 notilike() 的 intent 函数。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. not_inother Any ColumnOperators


实现 NOT IN 运算符。


这相当于使用 ColumnOperators.in_(),~x.in_(y)。


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


在 1.4 版本发生变更: not_in() 运算符从 notin_() 中。以前的名称仍然可用于向后兼容。


在 1.2 版本发生变更: ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认为空 IN 序列生成 “static” 表达式。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. not_likeother Any, escape:strNone=None ColumnOperators


实现 NOT LIKE 运算符。


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


在 1.4 版本发生变更: not_like() 运算符已从 notlike() 的以前的名称仍然可用于向后兼容。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. notilikeother Any, escape:strNone=None 列运算符


实现 NOT ILIKE 运算符。


这相当于使用 ColumnOperators.ilike()的 ColumnOperators.ilike()中,即 ~x.ilike(y)。


在 1.4 版本发生变更: not_ilike() 运算符已从 notilike() 的 intent 函数。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. notin_other Any ColumnOperators


实现 NOT IN 运算符。


这相当于使用 ColumnOperators.in_(),~x.in_(y)。


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


在 1.4 版本发生变更: not_in() 运算符从 notin_() 中。以前的名称仍然可用于向后兼容。


在 1.2 版本发生变更: ColumnOperators.in_()ColumnOperators.not_in() 运算符现在默认为空 IN 序列生成 “static” 表达式。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. notlikeother Any, escape:strNone=None 列运算符


实现 NOT LIKE 运算符。


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


在 1.4 版本发生变更: not_like() 运算符已从 notlike() 的以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. nulls_first ColumnOperators


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


在 1.4 版本发生变更: nulls_first() 运算符已从以前版本中的 nullsfirst() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. nulls_last ColumnOperators


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


在 1.4 版本发生变更: nulls_last() 运算符已从以前版本中的 nullslast() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. nullsfirst ColumnOperators


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


在 1.4 版本发生变更: nulls_first() 运算符已从以前版本中的 nullsfirst() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. nullslast ColumnOperators


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


在 1.4 版本发生变更: nulls_last() 运算符已从以前版本中的 nullslast() 重命名。以前的名称仍然可用于向后兼容。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. opopstring strprecedence int = 0is_comparison: bool = false, return_type:Type[TypeEngine[any]]TypeEngine[any]None=None, python_impl:Callable[...,Any]None=None) Callable[[any] operators []


继承自OperatorsOperators.op() 方法


生成泛型运算符函数。


例如:

somecolumn.op("*")(5)


生产:

somecolumn * 5


此函数还可用于使按位运算符显式。例如:

somecolumn.op("&")(0xFF)


somecolumn 中值的按位 AND。


参数

  • opstring – 一个字符串,将作为此元素和传递给生成函数的表达式之间的中缀运算符输出。


  • 优先级


    优先级,数据库应应用于 SQL 表达式中的运算符。此整数值充当 SQL 编译器的提示,以了解何时应在特定作周围呈现显式括号。较小的数字将导致表达式在应用于另一个优先级较高的运算符时被括起来。默认值 0 低于除逗号 () 和 AS 运算符之外的所有运算符。值 100 将高于或等于所有运算符,而 -100 将小于或等于所有运算符。


    另请参阅


    我正在使用 op() 生成自定义运算符,但我的括号没有正确显示 - SQLAlchemy SQL 编译器如何呈现括号的详细说明

  • is_comparison


    遗产;如果为 True,则该运算符将被视为 “比较” 运算符,即计算结果为布尔值 true/false 值,如 ==> 等。提供此标志是为了让 ORM 关系可以确定该运算符在自定义连接条件中使用时是比较运算符。


    使用 is_comparison 参数将被使用 Operators.bool_op() 方法;这个更简洁的运算符会自动设置此参数,但也提供正确的 PEP 484 类型支持,因为返回的对象将表示“布尔”数据类型,即 BinaryExpression[bool]。


  • return_type—— TypeEngine 类或对象,它将 force 此运算符生成的表达式的返回类型 属于该类型。 默认情况下,指定 Operators.op.is_comparison将解析为 Boolean 的 Boolean 和那些不 boolean 的 Boolean 将与左侧作数的类型相同。

  • python_impl


    一个可选的 Python 函数,该函数可以计算两个 Python 值,其工作方式与此运算符在数据库服务器上运行时的工作方式相同。对于 Python 内部 SQL 表达式评估函数(例如 ORM 混合属性)以及用于在多行更新或删除后匹配会话中的对象的 ORM“评估器”非常有用。


    例如:

    >>> expr = column("x").op("+", python_impl=lambda a, b: a + b)("y")


    上述表达式的运算符也适用于非 SQL 的 left 和 right 对象:

    >>> expr.operator(5, 10)
    15


    2.0 版的新Function。


method sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. operateop OperatorType*other Any**kwargs Any ColumnElement (列元素)[任意]


对参数进行作。


这是最低级别的作,加注 NotImplementedError 的 MethodS 错误。


在子类上覆盖 this 可以允许将通用行为应用于所有作。例如,重写 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()))传递。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. regexp_matchpattern Any, flags:strNone=None ColumnOperators


实现特定于数据库的 'regexp match' 运算符。


例如:

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 Database、PostgreSQL、MySQL 和 MariaDB 实施正则表达式支持。SQLite 提供部分支持。第三方方言之间的支持可能会有所不同。


参数

  • pattern– 正则表达式模式字符串或列子句。


  • flags– 要应用的任何正则表达式字符串标志,仅作为纯 Python 字符串传递。这些标志是特定于后端的。一些后端,如 PostgreSQL 和 MariaDB,也可以将标志指定为模式的一部分。在 PostgreSQL 中使用忽略大小写标志 'i' 时,将使用忽略大小写正则表达式匹配运算符 ~*!~*


在 1.4 版本加入.


在 1.4.48 版本发生变更: 2.0.18 请注意,由于实现错误,除了纯 Python 字符串之外,“flags”参数以前还接受 SQL 表达式对象,例如列表达式。此实现无法正确使用缓存,因此已被删除;只应为 “flags” 参数传递字符串,因为这些标志在 SQL 表达式中呈现为文本内联值。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. regexp_replacepattern Anyreplacement Any, flags:strNone=None ColumnOperators


实现特定于数据库的 'regexp replace' 运算符。


例如:

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


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


目前为 Oracle Database、PostgreSQL、MySQL 8 或更高版本以及 MariaDB 实施了正则表达式替换支持。第三方方言之间的支持可能会有所不同。


参数

  • pattern– 正则表达式模式字符串或列子句。


  • pattern– 替换字符串或 column 子句。


  • flags– 要应用的任何正则表达式字符串标志,仅作为纯 Python 字符串传递。这些标志是特定于后端的。一些后端,如 PostgreSQL 和 MariaDB,也可以将标志指定为模式的一部分。


在 1.4 版本加入.


在 1.4.48 版本发生变更: 2.0.18 请注意,由于实现错误,除了纯 Python 字符串之外,“flags”参数以前还接受 SQL 表达式对象,例如列表达式。此实现无法正确使用缓存,因此已被删除;只应为 “flags” 参数传递字符串,因为这些标志在 SQL 表达式中呈现为文本内联值。


属性 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. remote_attr


此引用的 'remote' 类属性 AssociationProxyInstance


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. reverse_operateop OperatorTypeother Any**kwargs Any 运算符


Reverse 对参数进行作。


用法与 operate() 相同。


attribute sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. 标量


如果此 AssociationProxyInstance 在本地端代理标量关系。


方法 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. startswithotherAny escape:strNone=None, autoescape bool = False 列运算符


实现 startswith 运算符。


生成一个 LIKE 表达式,该表达式针对字符串值开头的匹配项进行测试:

column LIKE <other> || '%'


例如:

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


由于运算符使用 LIKE、通配符 存在于 <other> 表达式中的 “%”“_” 的行为也类似于通配符。对于 Literals 字符串值,可以将 ColumnOperators.startswith.autoescape 该标志设置为 True 以对出现这些值 字符,以便它们与自身匹配 而不是通配符。 或者, ColumnOperators.startswith.escape parameter 将给定字符建立为转义字符,当目标表达式不是文本字符串时,该字符可能很有用。


参数

  • other —— 要比较的表达式。这通常是一个纯字符串值,但也可以是任意 SQL 表达式。默认情况下,除非标志 ColumnOperators.startswith.autoescape 设置为 True,否则不会对 LIKE 通配符 %_ 进行转义。


  • 自动转义


    布尔;当 True 时,建立转义字符 ,然后将其应用于 “%”、“_ 和比较值中的转义字符本身,该值假定为文本字符串,而不是 SQL 表达式。


    表达式,例如:

    somecolumn.startswith("foo%bar", autoescape=True)


    将渲染为:

    somecolumn LIKE :param || '%' ESCAPE '/'


    :p aram 的值为 “foo/%bar”。


  • 转义


    一个字符,当给定时,它将使用 ESCAPE 关键字将该字符建立为转义字符。然后,可以将此字符放在 %_ 的出现之前,以允许它们充当自身,而不是通配符。


    表达式,例如:

    somecolumn.startswith("foo/%bar", escape="^")


    将渲染为:

    somecolumn LIKE :param || '%' ESCAPE '^'


    该参数也可以与 ColumnOperators.startswith.autoescape

    somecolumn.startswith("foo%bar^bat", escape="^", autoescape=True)


    在上面,给定的 literal 参数将被转换为 “foo^%bar^^bat” 在传递到数据库之前。


属性 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. target_class: Type[Any]


由 this 处理的 intermediary 类 AssociationProxyInstance


拦截的 append/set/assignment 事件将导致生成此类的新实例。


属性 sqlalchemy.ext.associationproxy.ColumnAssociationProxyInstance. timetuple Literal[None] = None


Hack 允许在 LHS 上比较日期时间对象。


sqlalchemy.ext.associationproxy 中。AssociationProxyExtensionType(关联代理扩展类型)¶


一个枚举。


属性 sqlalchemy.ext.associationproxy.AssociationProxyExtensionType. ASSOCIATION_PROXY = 'ASSOCIATION_PROXY'


指示 AssociationProxy 类型的 InspectionAttr 的符号。


分配给 InspectionAttr.extension_type 属性。