本文详细介绍了 1.4 版本中进行的单个问题级别的更改。有关 1.4 中的新内容的叙述性概述,请参阅 SQLAlchemy 1.4 有什么新功能?。
无发布日期
发布日期:2024 年 3 月 4 日
with_loader_criteria()
不会应用到 Select.join()
的 bug,其中 ON 子句被给定为普通的 SQL 比较,而不是作为关系目标或类似的东西。
这是在 2.0 版本中修复的同一问题的回溯,针对 2.0.22。
参考:#10365
发布日期:2024 年 1 月 2 日
AsyncEngine.dispose()
会产生一个新的连接池,但没有完全重新建立使用 asyncio 兼容互斥锁的情况,导致在使用并发功能如 asyncio.gather()
时,在 asyncio 上下文中使用普通的 threading.Lock()
导致死锁。
参考:#10813
发布日期:2023 年 10 月 29 日
Select.join()
进行关系目标的子查询进行的问题。这些注解在特殊情况下使用子查询时使用,例如在 PropComparator.and_()
和其他 ORM 特定情况下。
参考资料:#10223
literal_execute=True
时多次使用相同的绑定参数会由于迭代问题导致渲染错误值的问题。
参考资料:#10142
Column
或其他 ColumnElement
的反序列化失败无法恢复正确的“比较器”对象的基本问题,该对象用于生成特定于类型对象的 SQL 表达式。
参考资料:#10213
Identity.order
参数的渲染,该参数是 Sequence
和 Identity
的一部分,不再对诸如 PostgreSQL 等其他后端有效。未来的版本将重新命名 Identity.order
、Sequence.order
和 Identity.on_null
参数,使用 Oracle 特定的名称,废弃旧名称,这些参数仅适用于 Oracle。
参考资料:#10207
connection.ping()
的False
参数,旨在禁用不需要的“自动重新连接”功能,在 MySQL 驱动程序和后端中已弃用,并且正在为某些版本的 MySQL 原生客户端驱动程序生成警告。对于 mysqlclient,它已被删除,而对于 PyMySQL 和基于 PyMySQL 的驱动程序,该参数将在某个时候被弃用和删除,因此 API 内省用于未来防范这些不同的删除阶段。
参考:#10492
发布日期:2023 年 7 月 5 日
ColumnOperators.regexp_match()
无法生成“稳定”缓存密钥的问题,即缓存密钥每次都会更改,导致缓存污染。对于带有标志和实际替换表达式的ColumnOperators.regexp_replace()
也存在相同的问题。现在,标志被表示为固定的修改器字符串,呈现为安全字符串,而不是绑定参数,并且替换表达式在“二进制”元素的主要部分内建立,以便生成适当的缓存密钥。
请注意,作为此更改的一部分,ColumnOperators.regexp_match.flags
和ColumnOperators.regexp_replace.flags
已修改为仅呈现为文字字符串,而以前它们呈现为完整的 SQL 表达式,通常是绑定参数。这些参数应始终作为纯 Python 字符串传递,而不是作为 SQL 表达式构造;不应该预期在实践中使用 SQL 表达式构造用于此参数,因此这是一个不兼容的更改。
此更改还修改了生成的表达式的内部结构,用于带有或不带有标志的ColumnOperators.regexp_replace()
以及带有标志的ColumnOperators.regexp_match()
。第三方方言可能已经实现了自己的正则表达式实现(在搜索中找不到这样的方言,所以预计影响较小),它们需要调整结构的遍历以适应。
参考:#10042
CacheKey
结构中的问题,其中__ne__()
运算符没有被正确实现,导致比较CacheKey
实例时得到荒谬的结果。
发布日期:2023 年 4 月 30 日
aliased()
和hybrid_property()
表达式组合的组合将导致缓存键不匹配,从而导致缓存键保留了实际的aliased()
对象,同时又不匹配等价构造的缓存键,填满了缓存。
参考:#9728
ORMExecuteState.is_column_load
、ORMExecuteState.is_relationship_load
、ORMExecuteState.loader_strategy_path
等)中,如果 SQL 语句本身是“复合选择”(如 UNION),则会抛出AttributeError
。
参考:#9634
lazy="selectinload"
,与另一个急加载器在相反的一侧结合使用时可能出现的无限循环。循环检查已修复以包括别名类关系。
引用:#9590
发布日期:2023 年 3 月 18 日
Update.values()
方法中与列相同名称的bindparam()
的错误/回归,以及Insert.values()
方法中与列相同名称的bindparam()
的错误/回归,仅在 2.0 版本中会在某些情况下静默地失败,不会遵守呈现参数的 SQL 表达式,而是用同名的新参数替换表达式并丢弃 SQL 表达式的任何其他元素,比如 SQL 函数等。具体情况是针对 ORM 实体而不是普通的Table
实例构建的语句,但如果语句在使用时被调用了一个Session
或一个Connection
,则会发生。
Update
的一部分问题既出现在 2.0 版本中又出现在 1.4 版本中,并且被回溯到 1.4 版本。
引用:#9075
CreateSchema
和DropSchema
DDL 构造的字符串化,当没有方言时会导致 AttributeError
。
引用:#7664
Operators.op()
自定义运算符函数不会生成适当的缓存键,导致 SQL 缓存的效果降低。
引用:#9506
@Backend.mapper_registry.mapped
),该表达式具有两个以上的组件,则会发生崩溃。现在会忽略这种情况;使用插件时,装饰器表达式需要是两个组件(即@reg.mapped
)。
参考文献:#9102
cursor.rowcount
值以用于 SELECT 语句。虽然这不是cursor.rowcount
的典型用法,但是其他 PostgreSQL 方言通常提供此值。拉取请求由 Michael Gorven 提供。
参考文献:#9048
mysql_length
字典。
参考文献:#9047
Table.schema
的参数,将不会在 SQL Server 方言的上下文中解释为解释为标记定界符的文档化行为,首次在#2626 中添加,当在反射操作中引用模式名称时。有关#2626 行为的最初假设是,只有在存在点时,方括号的特殊解释才是重要的,但是在实践中,由于这些不是常规或定界标识符中的有效字符,因此在所有 SQL 渲染操作中都不包括方括号作为标识符名称的一部分。拉取请求由 Shan 提供。
参考文献:#9133
ROWID
添加到反射类型中,因为此类型可能在“CREATE TABLE”语句中使用。
参考文献:#5047
发布日期:2023 年 1 月 3 日
SQLALCHEMY_WARN_20
环境变量未设置时,首次发出任何 SQLAlchemy 2.0 弃用警告时,将发出新的弃用“超级警告”。警告至多发出一次,然后设置一个布尔值以防止其再次发出。
此弃用警告旨在通知未在其要求文件中设置适当约束的用户,阻止对意外 SQLAlchemy 2.0 升级的惊喜,并提醒 SQLAlchemy 2.0 升级流程已经可用,因为预计很快将发布第一个完整的 2.0 版本。可以通过设置环境变量 SQLALCHEMY_SILENCE_UBER_WARNING
为 "1"
来关闭弃用警告。
另请参阅
SQLAlchemy 2.0 - 主要迁移指南
参考:#8983
platform.architecture()
来检测某些系统属性的问题,这导致针对一些情况下不可用的系统级 file
调用发生了过度广泛的系统调用,包括在某些安全环境配置中。
参考:#8995
Update
和 Delete
)的内部 SQL 遍历中的问题,该问题可能会导致与 ORM 更新/删除功能一起使用 lambda 语句时出现特定问题,以及其他潜在问题。
参考:#9033
Timeout
条件时发生,并且连接池检出由于超时而中断时未能清理失败状态,导致底层连接记录以及有时数据库连接本身“泄漏”,使得连接池处于无效状态,其中某些条目无法访问。该问题首次在 SQLAlchemy 1.2 中被识别并修复,针对 #4225,然而,在该修复中检测到的故障模式未能适应于 BaseException
,而不是 Exception
,这导致无法捕获 eventlet/gevent Timeout
。此外,还发现并加固了初始连接池连接中的一个块,使用了 BaseException
-> “清理失败连接” 块,以适应此位置中的相同条件。非常感谢 Github 用户 @niklaus 在识别和描述此复杂问题方面的坚持努力。
参考:#8974
FunctionElement.column_valued.joins_implicitly
, 这在使用表值或列值函数时防止“笛卡尔积”警告时非常有用。此参数已经为 FunctionElement.table_valued()
在 #7845 中引入,但未能为 FunctionElement.column_valued()
添加。
参考:#9009
TypeEngine.bind_expression()
,在与 literal_binds
编译器参数一起使用时处于“扩展”(即“IN”)参数的上下文中时。
参考:#8989
JSON
元素的比较等)缺乏类型信息。
参考:#9029
MACADDR8
。Pull 请求由 Asim Farooq 提供。
参考:#8393
Insert.on_conflict_do_update.constraint
参数接受 Index
对象的 bug,然而,它不会将此索引展开为其各个索引表达式,而是将其名称渲染到 ON CONFLICT ON CONSTRAINT 子句中,这不被 PostgreSQL 接受;“约束名”形式只接受唯一或排除约束名。该参数仍然接受索引,但现在将其展开为其组成表达式以进行渲染。
参考:#9023
index_list
pragma 命令未返回当前预期的列数,导致在反射表和索引时引发异常。
参考:#8969
unusual_column_name_characters
,可以关闭不支持带有点、斜杠或百分号等特殊字符的列名的第三方方言,即使名称已经正确引用。
参考:#9002
发布日期:2022 年 12 月 10 日
Session.merge()
无法保留使用relationship.viewonly
参数指示的关系属性的当前加载内容的错误,从而破坏了使用Session.merge()
从缓存和其他类似技术中拉取完全加载的对象的策略。在相关变更中,修复了一个问题,即包含已加载的关系但在映射上仍配置为lazy='raise'
的对象在传递给Session.merge()
时会失败;在合并过程中暂停了对“raise”的检查,假定Session.merge.load
参数保持其默认值True
。
总的来说,这是对 1.4 系列中引入的一项变更的行为调整,截至#4994,该变更将“merge”从默认应用于“viewonly”关系的级联集中移除。由于“viewonly”关系在任何情况下都不会持久化,允许它们的内容在“merge”期间传输不会影响目标对象的持久化行为。这使得Session.merge()
能够正确地满足其中一个用例,即向Session
中添加在其他地方加载的对象,通常是为了从缓存中恢复。
参考:#8862
with_expression()
中的问题,在这种情况下,由从封闭 SELECT 引用的列组成的表达式在某些情境下不会正确渲染 SQL,即使表达式具有与使用query_expression()
的属性匹配的标签名称,即使query_expression()
没有默认表达式。目前,如果query_expression()
确实有默认表达式,那个标签名称仍然用于该默认表达式,并且具有相同名称的额外标签将继续被忽略。总的来说,这种情况相当棘手,可能需要进一步调整。
参考:#8881
Result.freeze()
方法无法用于使用text()
或Connection.exec_driver_sql()
的文本 SQL 的问题。
参考:#8963
regexp_replace()
函数,首次引入于#4123的CTE
构造的“嵌套”功能,以及使用 Oracle 的FunctionElement.column_valued()
方法形成的可选择表。
参考:#8827
AsyncResult
中删除了无效的 merge()
方法。此方法从未起作用,并且错误地包含在 AsyncResult
中。
参考:#8952
format_type()
函数返回 NULL 的替代后端。
参考:#8748
FunctionElement.column_valued()
语法不正确的问题,未正确限定源表,导致名称 COLUMN_VALUE
不正确。
参考:#8945
发布日期:2022 年 11 月 12 日
Table
对象一次性使用 insert(some_table).values(...).returning(some_table)
的 INSERT 语句会失败执行,引发异常。
--disable-asyncio
参数时的问题,该参数实际上未能运行 greenlet 测试,并且也未能阻止套件在整个运行过程中使用“包装” greenlet。设置此参数后,确保整个运行过程中不会发生任何 greenlet 或 asyncio 使用。
参考:#8793
发布日期:2022 年 11 月 4 日
Select
构造的错误,其中 Select.select_from()
与 Select.join()
的组合,以及使用 Select.join_from()
时,如果查询的列子句未明确包括 JOIN 的左侧实体,则会导致 with_loader_criteria()
功能以及单表继承查询所需的 IN 条件不会呈现。现在正确的实体被传递给内部生成的 Join
对象,以便正确添加与左侧实体的条件。
引用:#8721
Load.options()
中使用它时,现在会引发一个信息性异常。此使用不受支持,因为 with_loader_criteria()
仅用作顶级加载器选项。以前,会生成内部错误。
引用:#8711
Session.get()
改进了“字典模式”,以便于在命名字典中指示指向主键属性名称的同义词名。
引用:#8753
Mapper.polymorphic_on
参数引用的 SQL 表达式不直接映射到类上,则此问题将无法正确工作。
引用:#8704
Query
对象作为迭代器时,如果在迭代过程中引发了用户定义的异常情况,则底层的 DBAPI 游标不会被关闭的问题,从而导致迭代器被 Python 解释器关闭。当使用Query.yield_per()
创建服务器端游标时,这将导致通常与服务器端游标不同步相关的 MySQL 问题,并且由于无法直接访问Result
对象,最终用户代码无法访问游标以关闭它。
为了解决这个问题,在迭代器方法中应用了对GeneratorExit
的捕获,当迭代器被中断时,将关闭结果对象,并且根据定义将被 Python 解释器关闭。
作为 1.4 系列实施的这一变更的一部分,确保了所有Result
实现都提供了.close()
方法,包括ScalarResult
、MappingResult
。2.0 版本的这一变更还包括了用于与Result
类一起使用的新上下文管理器模式。
参考:#8710
Connection
关闭并正在将其 DBAPI 连接返回到连接池时,在某些情况下不会调用PoolEvents.reset()
事件钩子的问题。
情况是当Connection
在将连接返回到池的过程中已经发出了.rollback()
时,然后会指示连接池放弃执行自己的“重置”以节省额外的方法调用。然而,这阻止了在此钩子中使用自定义池重置方案,因为这样的钩子定义上做的不仅仅是调用.rollback()
,并且需要在所有情况下被调用。这是在 1.4 版本中出现的一个退化。
对于版本 1.4,PoolEvents.checkin()
仍然可以作为用于自定义“重置”实现的替代事件钩子。版本 2.0 将提供一个改进版本的PoolEvents.reset()
,它被用于其他情况,例如终止 asyncio 连接,并且还传递了有关重置的上下文信息,以允许“自定义连接重置”方案以不同的方式响应不同的重置情况。
参考:#8717
Result
对象都包括Result.close()
方法以及Result.closed
属性,包括ScalarResult
和MappingResult
。
参考:#8710
literal_column()
构造在Select
构造上正常工作,以及在其他潜在的可能生成“匿名标签”的地方,如果字面表达式包含可能干扰格式字符串的字符,例如括号,由于“匿名标签”结构的实现细节。
参考:#8724
Inspector.has_table()
针对带有 SQL Server 方言的临时表时会在某些 Azure 变体上失败的问题,因为不支持在这些服务器版本上的不必要的信息模式查询。来自 Mike Barry 的拉取请求。
参考:#8714
Inspector.has_table()
的问题,由于 1.4 系列中去除了对 SQL Server 的此支持而导致错误返回False
。此问题在使用不同反射架构的 2.0 系列中不存在。添加了测试支持以确保has_table()
保持符合视图的规范。
引用:#8700
nls_session_parameters
视图以获取默认的小数点字符可能不可用,具体取决于 Oracle 连接模式,因此可能会引发错误。检测小数字符的方法已经简化为直接测试一个小数值,而不是读取系统视图,这在任何后端/驱动程序上都有效。
引用:#8744
发布日期:2022 年 10 月 16 日
Session.execute()
等方法时,Session.execute.bind_arguments
字典不再发生变化;相反,它会被复制到内部字典以进行状态更改。在其他方面,这修复了一个问题,即传递给Session.get_bind()
方法的“clause”将错误地引用了用于“fetch”同步策略的Select
构造,而实际发出的查询是Delete
或Update
。这会干扰“路由会话”的处理方法。
引用:#8614
remote()
注解。理想情况下,这在某个时候会引发错误,因为从映射的角度来看这是不正确的。
参考文献:#7094
contains_eager()
中修复了 1.4 版本的回归问题,在其中 joinedload()
的“包装成子查询”逻辑会意外地触发使用 contains_eager()
函数的类似语句(例如使用 distinct()
、limit()
或 offset()
的语句),然后会导致使用一些 SQL 标签名称和别名的查询的次要问题。这种“包装”对于 contains_eager()
并不合适,因为它一直遵循的合同是用户定义的 SQL 语句未经修改,只是添加了适当的列以被提取。
参考文献:#8569
select()
构造的列子句中混合使用“*”和额外显式命名列表达式会导致结果列定位有时将标签名称或其他非重复名称视为模糊目标的问题。
参考文献:#8536
asyncio.shield()
在上下文管理器中的实现,如在 #8145 中所添加的,使得“关闭”操作被封装在一个 asyncio.Task
中,然后在操作进行时被强引用。这符合 Python 文档中指出的任务否则不会被强引用的情况。
参考文献:#8516
aggregate_order_by
现在支持缓存生成。
参考文献:#8574
system_views
视图。错误捕获已扩展,保证这种方法绝对不会失败,只要有数据库连接存在。
参考文献:#8525
发布日期:2022 年 9 月 6 日
Session
类关联事件之后创建的 sessionmaker
类。
参考文献:#8467
aliased()
和with_polymorphic()
构造的缓存键策略。虽然很难(如果有的话)演示涉及实际语句被缓存的问题,但是这两个构造在其缓存键中未包含足够使它们在缓存时独特的内容,以使仅对别名构造进行缓存是准确的。
参考:#8401
sqlalchemy.ext.mutable
扩展中的问题,如果对象在通过 Session.merge()
合并时同时传递 Session.merge.load
为 False,则会丢失到父对象的集合链接。
参考:#8446
with_loader_criteria()
的问题,其中在 lambda 内使用的闭包变量作为绑定参数值在语句被缓存后不会正确传递到额外的关系加载器(如selectinload()
和lazyload()
),而是使用过时的原始缓存值。
参考:#8399
table()
构造时,将字符串传递给 table.schema
参数时,未考虑“schema”字符串在生成缓存键时,导致如果使用了具有不同模式的多个同名 table()
构造,可能会导致缓存冲突的问题。
参考:#8441
terminate()
方法调用的支持,用于在连接池回收可能超时的连接、在垃圾回收未正常关闭连接时以及连接已失效时。这允许 asyncpg 放弃连接而无需等待可能导致长时间超时的响应。
参考:#8419
dm_exec_sessions
或 dm_pdw_nodes_exec_sessions
系统视图以确定当前事务隔离级别时,连接会失败。
参考:#8475
发布日期:2022 年 8 月 8 日
SELECT *
参数(通过字符串、text()
或 literal_column()
)给 select()
构造,将被解释为核心级别的 SQL 语句,而不是 ORM 级别的语句。这样,当 *
扩展以匹配任意数量的列时,将返回结果中的所有列。ORM 级别的 select()
的解释需要提前知道所有 ORM 列的名称和类型,而当使用 '*'
时无法实现。
如果在 ORM 语句中同时使用'*
和其他表达式,会引发错误,因为 ORM 无法正确解释这种情况。
参考:#8235
declared_attr
可调用的类上,该类希望在后代类上使用它们。
参考:#8190
Connection
实现了新的Connection.execution_options.yield_per
执行选项,以模仿 ORM 中相同的 yield_per 选项。该选项同时设置Connection.execution_options.stream_results
选项,并调用Result.yield_per()
,以提供最常见的流式结果配置,也与 ORM 使用情况中的使用模式相一致。
另请参阅
使用服务器端游标(即流式结果) - 修订文档
Result
中的错误,当使用缓冲结果策略时,如果使用的方言不支持显式的“服务器端游标”设置,则不会使用Connection.execution_options.stream_results
。这是一个错误,因为像 SQLite 和 Oracle 这样的 DBAPI 已经使用了非缓冲结果获取方案,仍然受益于部分结果获取的使用。在设置Connection.execution_options.stream_results
的所有情况下现在都使用“缓冲”策略。
FilterResult.yield_per()
,以便结果实现(如 MappingResult
、ScalarResult
和 AsyncResult
)可以访问此方法。
参考:#8199
.contains()
、.startswith()
、.endswith()
的 SQL 编译,强制使用字符串连接运算符,而不是依赖于加法运算符的重载,以便非标准的用法,例如字节字符串仍然生成字符串连接运算符。
参考:#8253
AsyncConnection
或 AsyncSession
作为上下文管理器释放对象时,特别是在 __aexit__()
上下文管理器退出时,将 asyncio.shield()
添加到连接和会话释放过程中。 当上下文管理器完成时,这似乎有助于使用其他并发库(如 anyio
、uvloop
)时取消任务时正确释放连接池中的连接。
参考:#8145
host:port
对作为查询字符串传递,例如 ?host=host1:port1&host=host2:port2&host=host3:port3
,但没有正确实现,因为它没有适当地传播“port”参数。 没有使用不同“port”的连接可能正常工作,而具有某些条目的“port”可能会不正确地传递该主机名。 现在已更正格式以正确传递主机/端口。
作为这一变化的一部分,保持了另一种意外有效的多主机样式的支持,即以逗号分隔的?host=h1,h2,h3&port=p1,p2,p3
。这种格式更符合 libpq 的查询字符串格式,而以前的格式受到 libpq URI 格式的不同方面的启发,但并不完全相同。
如果两种样式混合在一起,会引发错误,因为这是模棱两可的。
参考:#4392
ignore_no_transaction_on_rollback
,指示回滚应忽略 Azure Synapse 错误‘No corresponding transaction found. (111214)’,如果没有与 Python DBAPI 冲突的事务存在。
初版补丁和宝贵的调试协助由@ww2406 提供。
另见
避免 Azure Synapse Analytics 上的事务相关异常
参考:#8231
TypeDecorator
在装饰ARRAY
数据类型时,不会正确代理__getitem__()
运算符的问题,没有明确的解决方法。
参考:#7249
发布日期:2022 年 6 月 24 日
发布日期:2022 年 6 月 23 日
PropComparator.has()
或PropComparator.any()
与使用传统别名功能的联接继承查询。
参考:#8162
GenerativeSelect.fetch()
未被应用的问题。
参考:#8091
with_loader_criteria()
选项无法被 pickle,这在与缓存方案一起传播到懒加载器时是必要的。目前,唯一支持的可 pickle 形式是将“where criteria”作为一个固定的模块级可调用函数传递,该函数生成一个 SQL 表达式。一个临时的“lambda”无法被 pickle,而一个 SQL 表达式对象通常不能直接完全 pickle。
参考:#8109
Connection
等关键对象具有正确__weakref__
属性的弃用警告类装饰器,导致像 Python 标准库inspect.getmembers()
之类的操作失败。
参考:#8115
lambda_stmt()
相关的多个观察到的竞争条件,包括在多个同时线程中首次分析新的 Python 代码对象时出现的初始“dogpile”问题,这导致了性能问题以及一些内部状态的损坏。此外,修复了观察到的竞争条件,当在不同线程中编译或访问正在被克隆的表达式构造时可能发生,因为 Python 版本在 3.10 之前的版本中,由于记忆化属性在迭代时改变__dict__
,特别是 lambda SQL 构造对此很敏感,因为它持久地保留一个单一的语句对象。迭代已经被改进为使用dict.copy()
,无论是否有额外的迭代。
参考:#8098
Cast
和其他“包装”列构造的机制,以更完全地保留被包装的Label
构造,包括标签名称将在Subquery
的.c
集合中被保留。该标签已经能够在它被包装在内部的构造外正确地呈现 SQL。
参考:#8084
compiled.params
的转义名称,并向SQLCompiler.construct_params()
添加了一个条件参数,命名为escape_names
,默认为True
,以默认情况下恢复旧行为。
参考资料:#8113
Table.include_columns
和Table.resolve_fks
参数的错误;这些很少使用的参数显然无法为引用外键约束的列工作。
在第一种情况下,引用外键的未包含列仍然会尝试创建一个ForeignKey
对象,在尝试解析外键约束的列时会产生错误;引用被跳过的列的外键约束现在与具有相同条件的Index
和UniqueConstraint
对象一样在表反射过程中被省略了。但是不会产生警告,因为我们可能希望在 2.0 中删除所有约束的 include_columns 警告。
在后一种情况下,如果找不到与 FK 相关的表,则会在resolve_fks=False
的情况下无法生成表别名或子查询;逻辑已被修复,因此如果未找到相关表,则ForeignKey
对象仍会被代理到别名表或子查询(这些ForeignKey
对象通常用于生成连接条件),但会发送一个标志,表明它是不可解析的。然后,别名表/子查询将正常工作,唯一的例外是它无法自动生成连接条件,因为缺少外键信息。这已经是这种外键约束的行为,该约束是使用非反射方法产生的,例如从不同的MetaData
集合中连接Table
对象。
参考:#8100, #8101
Table
对象使用带有Numeric
数据类型的 IDENTITY 列时,尝试调解“autoincrement”列时会产生错误,阻止了使用Column.autoincrement
参数来构造Column
的构造,并在尝试调用Insert
构造时发出错误。
参考:#8111
Mutable
中的错误,在其中对包含多个Mutable
启用属性的映射实例进行 pickling 和 unpickling 时,将不会正确恢复状态。
参考:#8133
发布日期:2022 年 5 月 31 日
column_property()
构造包含子查询的问题,该子查询针对已映射的列属性将不正确应用 ORM 编译行为,包括针对单表继承表达式添加的“IN”表达式将无法包含的问题。
参考:#8064
Select.with_only_columns()
时,会向返回的 Row
对象应用不正确的键名。
参考:#8001
Insert.on_conflict_do_update()
方法和 SQLite 的 Insert.on_conflict_do_update()
方法中,当使用字典传递给 Insert.on_conflict_do_update.set_
时,如果通过其键名指定列时失败,以及如果直接使用 Insert.excluded
集合作为字典时也会失败,尤其是当列有单独的“.key”时。
参考:#8014
Insert.from_select()
传递一个“复合选择”对象,如 UNION,但 INSERT 语句需要附加额外的列以支持来自表元数据的 Python 端或显式 SQL 默认值时,将引发一个信息性错误。在这种情况下,应传递复合对象的子查询。
引用:#8073
bindparam()
没有明确给出数据或类型时,可能会在表达式中被强制转换为不正确类型的问题,例如当使用Comparator.any()
和Comparator.all()
时。
引用:#7979
BindParameter
对象共享相同的名称,但一个在“扩展”上下文中使用(通常是 IN 表达式),另一个则不是,则会引发一个信息性错误;在这两种不同风格的用法中混合使用相同的名称不受支持,通常应在要在 IN 表达式之外接收列表值的参数上设置expanding=True
参数(默认情况下设置expanding
)。
引用:#8018
Column
的名称本身需要引号引用时,因此在 DML 语句中生成的绑定参数使用需要绑定处理的数据类型时,引号引用的名称将用于绑定参数。
参考:#8053
发布日期:2022 年 4 月 26 日
Insert
构造部分被识别为 ORM 启用的语句,未正确传递正确的映射器/映射表状态给Session
,导致绑定到使用Session.binds
参数绑定到引擎和/或连接的Session
的Session.get_bind()
方法失败。
参考:#7936
DeclarativeMeta
元类,将cls.__dict__
传递到声明扫描过程中以查找属性,而不是传递给类型的__init__()
方法的单独字典。这允许用户定义的基类在__init_subclass__()
中添加属性时按预期工作,因为__init_subclass__()
只能影响cls.__dict__
本身,而不能影响其他字典。从技术上讲,这是从 1.3 版本中使用__dict__
的退化。
参考:#7900
Row
的命名成员时,如果成员不存在,可能会发生内存泄漏;特别是在 NumPy 转换期间尝试调用.__array__
等成员时,但问题围绕着Row
对象抛出的任何AttributeError
。这个问题不适用于已经过渡到 Cython 的 2.0 版本。非常感谢 Sebastian Berg 发现了这个问题。
参考:#7875
Result.columns()
方法中存在的 bug 的警告,当在与将返回单个 ORM 实体的Result
一起传递 0 时,这表明Result.columns()
方法的当前行为在这种情况下是错误的,因为Result
对象将产生标量值而不是Row
对象。这个问题将在 2.0 中修复,这将是一个向后不兼容的更改,对于依赖于当前错误行为的代码来说,这是一个向后不兼容的更改。想要接收标量值集合的代码应该使用Result.scalars()
方法,它将返回一个新的ScalarResult
对象,该对象产生非行标量对象。
参考:#7953
referred_column_0
命名约定键设置外键约束时,如果外键约束是作为一个ForeignKey
对象而不是一个明确的ForeignKeyConstraint
对象来设置时,命名约定将无效。由于此更改使用了一些从版本 2.0 中回退的修复的特性,还修复了一个很可能已经存在多年的、不为人所知的特性,即一个ForeignKey
对象可以仅通过表的名称而不使用列名来引用被引用的表,如果被引用列的名称与被引用列的名称相同的话。
先前未对 ForeignKey
对象进行测试的 referred_column_0
命名约定键,仅对 ForeignKeyConstraint
进行了测试,此 bug 揭示了除非对所有 FK 约束使用 ForeignKeyConstraint
,否则该功能从未正确工作。此 bug 可追溯到为 #3989 引入该功能。
参考:#7958
contextvar.ContextVar
对象的处理。先前,在非可等待代码中调用可等待项时,不会传播应用于 ContextVar
的值。
参考:#7937
ARRAY
数据类型与 Enum
结合时的 bug,其中使用 .any()
或 .all()
方法以 Python 枚举成员作为参数来渲染 SQL 的 ANY() 或 ALL(),会导致所有驱动程序的类型适配失败。
参考:#6515
UUID
类型对象实现了 UUID.python_type
属性。该属性将根据 UUID.as_uuid
参数设置返回 str
或 uuid.UUID
。此前,该属性未实现。感谢 Alex Grönholm 的拉取请求。
参考:#7943
create_engine.pool_pre_ping
参数时,psycopg2 方言中的问题,该问题会导致用户配置的 AUTOCOMMIT
隔离级别被“ping”处理程序意外重置。
参考:#7930
TypeError
。
参考:#7871
SimpleUpdateDeleteTest
套件测试的一个缺失要求,该套件测试未检查目标方言上的“rowcount”函数是否有效。
参考:#7919
发布日期:2022 年 4 月 6 日
FunctionElement.table_valued.joins_implicitly
功能中的错误,即参数不会自动从原始TableValuedAlias
对象传播到调用TableValuedAlias.render_derived()
或TableValuedAlias.alias()
时生成的次要对象。
另外还修复了TableValuedAlias
中的这些问题:
TableValuedAlias.render_derived()
时可能发生的问题(对于.alias(),我们目前仍然必须继续从前一个元素链接。不确定是否可以改进,但这是.alias()在其他地方的标准行为)
TableValuedAlias.render_derived()
或TableValuedAlias.alias()
时个别元素类型丢失的问题。
参考:#7890
发布日期:2022 年 3 月 31 日
Session.execute()
直接调用包含 ORM 实体的Insert
构造会失败的问题。
参考:#7878
发布日期:2022 年 3 月 31 日
with_polymorphic.adapt_on_names
到with_polymorphic()
函数,允许针对将仅根据列名适应到原始映射可选择项的替代可选择项进行多态加载(通常使用具体映射)。
参考:#7805
UpdateBase.returning_column_descriptions
和UpdateBase.entity_description
,允许检查作为Insert
、Update
或Delete
构造的 ORM 属性和实体。对于仅限于 Core 的可选择项,现在还实现了Select.column_descriptions
访问器。
参考:#7861
Query.filter_by()
方法在查询的关系中存在“次要”表且映射针对复杂内容(如“with polymorphic”)时,不会给出适当的实体进行过滤。
参考:#7868
composite()
属性与连接表继承的selectin_polymorphic()
加载策略不兼容的错误。
参考:#7801
selectin_polymorphic()
加载选项在没有固定“polymorphic_on”列的连接继承映射器上无法工作的问题。此外,还增加了对此结构的更广泛用法模式的测试支持。
参考:#7799
with_loader_criteria()
函数中的错误,其中加载条件不会应用于在父对象的刷新操作范围内调用的连接急加载。
参考:#7862
Mapper
在映射到UNION
时过于激进地减少用户定义的Mapper.primary_key
参数的错误,其中对于某些 SELECT 条目,两列本质上是等效的,但在另一个条目中,它们不是,例如在递归 CTE 中。这里的逻辑已更改为接受给定的用户定义 PK,其中列将与映射的可选择相关联,但不再“减少”,因为这种启发式无法适应所有情况。
参考:#7842
Engine.dispose.close
,默认为 True。当为 False 时,引擎处理不会触及旧池中的连接,只是丢弃池并替换它。这种用例是为了当原始池从父进程传输时,父进程可以继续使用这些连接。
另请参阅
使用连接池与多进程或 os.fork() - 修订文档
参考:#7815, #7877
Engine
级别的 create_engine.isolation_level
参数时的情况。
参考:#7853
FunctionElement.table_valued.joins_implicitly
, 用于 FunctionElement.table_valued()
结构。此参数指示提供的表值函数将自动与引用的表执行隐式连接。这实际上禁用了“from linting”功能,例如由于存在此参数而触发的“笛卡尔积”警告。可用于函数,如 func.json_each()
。
参考:#7845
bindparam.literal_execute
参数现在参与了 bindparam()
的缓存生成,因为它更改了编译器生成的 sql 字符串。以前使用了正确的绑定值,但是在相同查询的后续执行中会忽略 literal_execute
。
参考:#7876
TextualSelect
的新功能未在编译器中完全实现,导致与 CTE 和文本语句结合时出现“INSERT FROM SELECT”和“INSERT…ON CONFLICT”等复合 INSERT 结构的问题。
参考:#7798
Table.to_metadata()
的Table.to_metadata.referred_schema_fn
可调用对象可以返回值BLANK_SCHEMA
以指示参考外键应重置为 None。该函数还可以返回RETAIN_SCHEMA
符号以指示“无更改”,这将与当前行为相同,当前行为也表示无更改。
参考资料:#7860
association_proxy()
构造尝试在类级别访问目标属性且此访问失败时引发的错误消息。这里的特定用例是当代理到不包括工作类级实现的混合属性时。
参考资料:#7827
发布日期:2022 年 3 月 6 日
FlushError
,它比 1.3 的以前的“空标识”异常更早地被引发,因为实际 INSERT 的行数不符合预期的情况在 1.4 中是一个更关键的情况,因为它阻止了多个对象的批处理正确工作。这与新提取的主键值被提取为 NULL 的情况不同,后者继续引发现有的“空标识”异常。
参考:#7594
relationship()
中使用完全合格路径的类名的问题,尽管其中包含了不正确的路径标记名称,但不是第一个标记,但是失败不会引发具有信息的错误,而是在稍后的步骤中随机失败。
参考:#7697
Engine
,Connection
以建立适当的堆栈级别参数,这样当在自定义日志格式化程序中使用 Python 日志标记funcName
和lineno
时,将报告正确的信息,这在过滤日志输出时可能很有用;在 Python 3.8 及以上版本上支持。感谢 Markus Gerstel 的贡献的拉取请求。
参考:#7612
SET
数据类型以及通用 Enum
数据类型中的问题,在这些类型的__repr__()
方法不会在字符串输出中呈现所有可选参数,影响这些类型在 Alembic 自动生成中的使用。MySQL 的拉取请求由 Yuki Nishimine 提供。
参考:#7598, #7720, #7789
Enum.length
参数而没有同时指定Enum.native_enum
为 False,Enum
数据类型会发出警告,因为在这种情况下参数将被静默忽略,尽管Enum
数据类型仍会在没有原生 ENUM 数据类型的后端(如 SQLite)上渲染 VARCHAR DDL。这种行为在未来的发布中可能会更改,以便无论“native_enum”设置如何,“length”都会适用于所有非本机“enum”类型。
TextualSelect
实例上调用HasCTE.add_cte()
方法时,SQL 编译器未进行适配的问题。此修复还将更多“SELECT”类似的编译器行为添加到TextualSelect
中,包括可容纳 DML CTEs(如 UPDATE 和 INSERT)。
参考:#7760
Result
对象的流式结果执行选项Connection.execution_options.stream_results
时,AsyncSession.execute()
方法未引发信息丰富的异常的问题。在这种情况下,现在会引发异常,就像在与AsyncConnection.execute()
方法一起使用Connection.execution_options.stream_results
选项时已引发异常一样。
此外,为了提高与状态敏感的数据库驱动程序(如 asyncmy)的稳定性,当出现此错误条件时,游标现在会被关闭;以前在 asyncmy 方言中,连接会进入无效状态,服务器端结果仍未消耗。
参考:#7667
NOT VALID
短语的编译器支持,用于渲染 CheckConstraint
、ForeignKeyConstraint
和 ForeignKey
架构构造的 DDL。感谢 Gilbert Gilb 的拉取请求。
另请参阅
PostgreSQL 约束选项
参考:#7600
CursorResult.inserted_primary_key
属性不可用。该方言现在主动获取此值,以适用的情况。
参考:#7738
[]
或 ```py, which are discarded by the database when producing the column name.
References: #7736
"_id"
, would not correctly track a Python generated default value due to the bound-parameter rewriting missing this value, causing an Oracle error to be raised.
References: #7676
DPI-1080
and DPI-1010
, both of which now indicate a disconnect scenario as of cx_Oracle 8.3.
References: #7748
-W
parameter and SQLAlchemy’s test suite will continue to pass. Additionally, modernized the detection of the “pytest-xdist” plugin so that plugins can be globally disabled using PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 without breaking the test suite if xdist were still installed. Warning filters that promote deprecation warnings to errors are now localized to SQLAlchemy-specific warnings, or within SQLAlchemy-specific sources for general Python deprecation warnings, so that non-SQLAlchemy deprecation warnings emitted from pytest plugins should also not impact the test suite.
References: #7599
Released: January 20, 2022
Session.bulk_save_objects()
where the sorting that takes place when the preserve_order
parameter is set to False would sort partially on Mapper
objects, which is rejected in Python 3.11.
References: #7591
FILESTREAM
when using VARBINARY(max)
in MSSQL.
See also
VARBINARY.filestream
References: #7243
Released: January 19, 2022
registry.map_imperatively()
more than once for the same class would produce an unexpected error, rather than an informative error that the target class is already mapped. This behavior differed from that of the mapper()
function which does report an informative message already.
References: #7579
AsyncSession.invalidate()
to the AsyncSession
class.
References: #7524
polymorphic_load="selectin"
option as well as relationship lazy loading, leading to a TypeError
.
References: #7557
aliased()
function against an existing aliased()
construct would fail to produce correct SQL if the existing construct were against a fixed table. The fix allows that the original aliased()
construct is disregarded if it were only against a table that’s now being replaced. It also allows for correct behavior when constructing a aliased()
without a selectable argument against a aliased()
that’s against a subuquery, to create an alias of that subquery (i.e. to change its name).
The nesting behavior of aliased()
remains in place for the case where the outer aliased()
object is against a subquery which in turn refers to the inner aliased()
object. This is a relatively new 1.4 feature that helps to suit use cases that were previously served by the deprecated Query.from_self()
method.
References: #7576
Select.correlate_except()
method, when passed either the None
value or no arguments, would not correlate any elements when used in an ORM context (that is, passing ORM entities as FROM clauses), rather than causing all FROM elements to be considered as “correlated” in the same way which occurs when using Core-only constructs.
References: #7514
Query.from_statement()
or Select.from_statement()
. As subqueryload requires modifying the original statement, it’s not compatible with the “from_statement” use case, especially for statements made against the text()
construct. The behavior now is equivalent to that of 1.3 and previously, which is that the loader strategy silently degrades to not be used for such statements, typically falling back to using the lazyload strategy.
References: #7505
TypeEngine
implementations from Python literals to apply a second level of adjustment to the type, so that a Python datetime with or without tzinfo can set the timezone=True
parameter on the returned DateTime
object, as well as Time
. This helps with some round-trip scenarios on type-sensitive PostgreSQL dialects such as asyncpg, psycopg3 (2.0 only).
References: #7537
Var
instance.
References: #7321
AdaptedConnection.run_async()
to the DBAPI connection interface used by asyncio drivers, which allows methods to be called against the underlying “driver” connection directly within a sync-style function where the await
keyword can’t be used, such as within SQLAlchemy event handler functions. The method is analogous to the AsyncConnection.run_sync()
method which translates async-style calls to sync-style. The method is useful for things like connection-pool on-connect handlers that need to invoke awaitable methods on the driver connection when it’s first created.
See also
Using awaitable-only driver methods in connection pool and other events
References: #7580
UUID
datatype, so that stringifying a statement with “literal_binds” that uses this type will render an appropriate string value for the PostgreSQL backend. Pull request courtesy José Duarte.
References: #7561
include_columns
as part of the dialect_options
entry in the reflected index dictionary, thereby enabling round trips from reflection->create to be complete. Included columns continue to also be present under the include_columns
key for backwards compatibility.
References: #7382
SHOW VARIABLES LIKE
statement with equivalent SELECT @@variable
in MySQL and MariaDB dialect initialization. This should avoid mutex contention caused by SHOW VARIABLES
, improving initialization performance.
References: #7518
Released: December 22, 2021
Session.get.execution_options
parameter which was previously missing from the Session.get()
method.
References: #7410
PropComparator.and_()
where usage with a loader strategy like selectinload()
against a column that was a member of the .c.
collection of a subquery object, where the subquery would be dynamically added to the FROM clause of the statement, would be subject to stale parameter values within the subquery in the SQL statement cache, as the process used by the loader strategy to replace the parameters at execution time would fail to accommodate the subquery when received in this form.
References: #7489
with_loader_criteria()
feature or the the PropComparator.and_()
method within a loader strategy in conjunction with a subquery which referred to the same entity being altered by the criteria option, or loaded by the loader strategy. A check for coming across the same loader criteria option in a recursive fashion has been added to accommodate for this scenario.
References: #7491
__class_getitem__()
method of the generated declarative base class by as_declarative()
would lead to inaccessible class attributes such as __table__
, for cases where a Generic[T]
style typing declaration were used in the class hierarchy. This is in continuation from the basic addition of __class_getitem__()
in #7368. Pull request courtesy Kai Mueller.
References: #7368, #7462
lazyload(aliased(A).bs).joinedload(B.cs)
would fail to result in the joinedload being invoked for runs subsequent to the query being cached, due to a mismatch for the options / object path applied to the objects loaded for a query with a lead entity that used aliased()
.
References: #7447
AttributeError
that’s raised when attempting to write to an attribute on the Row
class, which is immutable. The previous message claimed the column didn’t exist which is misleading.
References: #7432
make_url()
function used to parse URL strings where the query string parsing would go into a recursion overflow if a Python 2 u''
string were used.
References: #7446
__builtins__
and other un-locatable or unqualified names that previously had not raised any assertions.
References: #7496
async_engine_config()
function to create an async engine from a configuration dict. This otherwise behaves the same as engine_from_config()
.
References: #7301
mariadbconnector
dialect, which was failing for disconnects that occurred due to common MySQL/MariaDB error codes such as 2006; the DBAPI appears to currently use the mariadb.InterfaceError
exception class for disconnect errors such as error code 2006, which has been added to the list of classes checked.
References: #7457
CompareAndCopyTest::test_all_present
would fail on some platforms due to additional testing artifacts being detected. Pull request courtesy Nils Philippsen.
References: #7450
Released: December 9, 2021
setup.py
still has fallback imports to distutils, as SQLAlchemy 1.4 does not have a hard setuptools versioning requirement at this time. SQLAlchemy 2.0 is expected to use a full PEP 517 installation layout which will indicate appropriate setuptools versioning up front.
References: #7311
PropComparator.any()
method on a relationship()
in the case where the related class also makes use of ORM polymorphic loading, would fail if a hybrid property on the related, polymorphic class were used within the criteria for the any()
operation.
References: #7425
as_declarative()
decorator and similar functions used to generate the declarative base class would not copy the __class_getitem__()
method from a given superclass, which prevented the use of pep-484 generics in conjunction with the Base
class. Pull request courtesy Kai Mueller.
References: #7368
Query
or Select
were used to load multiple kinds of entities, along with loader options that apply to just one of those kinds of entity like a joinedload()
, and later the objects would be refreshed from expiration, where the loader options would attempt to be applied to the mismatched object type and then raise an exception. The check for this mismatch now bypasses raising an error for this case.
References: #7318
UserDefinedOption
, by definition are handled on every statement execution and do not need to be considered as part of the cache key for the statement. Caching of the base ExecutableOption
class has been modified so that it is no longer a HasCacheKey
subclass directly, so that the presence of user defined option objects will not have the unwanted side effect of disabling statement caching. Only ORM specific loader and criteria options, which are all internal to SQLAlchemy, now participate within the caching system.
References: #7394
synonym()
and potentially other kinds of “proxy” attributes would not in all cases successfully generate a cache key for their SQL statements, leading to degraded performance for those statements.
References: #7394
relationship()
would go into an endless loop if in-place added to itself, i.e. the +=
operator were used, as well as if .extend()
were given the same list.
References: #7389
Session
were to close the connection within the Session.commit()
method, when using a context manager for Session.begin()
, it would attempt a rollback which would not be possible as the Session
was in between where the transaction is committed and the connection is then to be returned to the pool, raising the exception “this sessiontransaction is in the committed state”. This exception can occur mostly in an asyncio context where CancelledError can be raised.
References: #7388
".*"
, which appears to be no different than passing a single asterisk, and will emit a deprecation warning if used. This syntax may have been intended for something but there is currently no need for it.
References: #4390
copy()
and deepcopy()
to the URL
class. Pull request courtesy Tom Ritchford.
References: #7400
Select.union()
, Select.intersect_all()
etc. now accept *other
as an argument rather than other
to allow for multiple additional SELECTs to be compounded with the parent statement at once. In particular, the change as applied to CTE.union()
and CTE.union_all()
now allow for a so-called “non-linear CTE” to be created with the CTE
construct, whereas previously there was no way to have more than two CTE sub-elements in a UNION together while still correctly calling upon the CTE in recursive fashion. Pull request courtesy Eric Masseran.
References: #7259
Exists.where()
method, unifying the api with the one presented by a normal select()
construct.
References: #7386
TypeDecorator.cache_ok
attribute and corresponding warning message if this flag is not defined, a behavior first established for TypeDecorator
as part of #6436, to also take place for UserDefinedType
, by generalizing the flag and associated caching logic to a new common base for these two types, ExternalType
to create UserDefinedType.cache_ok
.
The change means any current UserDefinedType
will now cause SQL statement caching to no longer take place for statements which make use of the datatype, along with a warning being emitted, unless the class defines the UserDefinedType.cache_ok
flag as True. If the datatype cannot form a deterministic, hashable cache key derived from its arguments, the attribute may be set to False which will continue to keep caching disabled but will suppress the warning. In particular, custom datatypes currently used in packages such as SQLAlchemy-utils will need to implement this flag. The issue was observed as a result of a SQLAlchemy-utils datatype that is not currently cacheable.
See also
ExternalType.cache_ok
References: #7319
[no key]
to be logged for elements which made use of these.
References: #7394
declared_attr
methods for non-mapped names like __mapper_args__
, __table_args__
, or other dunder names, as the plugin would try to interpret these as mapped attributes which would then be later mis-handled. As part of this change, the decorated function is still converted by the plugin into a generic assignment statement (e.g. __mapper_args__: Any
) so that the argument signature can continue to be annotated in the same way one would for any other @classmethod
without Mypy complaining about the wrong argument type for a method that isn’t explicitly @classmethod
.
References: #7321
hstore
and array
constructs which would cause [no key]
to be logged for these elements.
References: #7394
Released: November 11, 2021
aliased()
construct directly in a second loader option, such as selectinload(A.aliased_bs).joinedload(aliased_b.cs)
, without explicitly qualifying using PropComparator.of_type()
on the preceding element of the path. Additionally, targeting the non-aliased class directly would be accepted (inappropriately), but would silently fail, such as selectinload(A.aliased_bs).joinedload(B.cs)
; this now raises an error referring to the typing mismatch.
References: #7224
Result
objects will now consistently raise ResourceClosedError
if they are used after a hard close, which includes the “hard close” that occurs after calling “single row or value” methods like Result.first()
and Result.scalar()
. This was already the behavior of the most common class of result objects returned for Core statement executions, i.e. those based on CursorResult
, so this behavior is not new. However, the change has been extended to properly accommodate for the ORM “filtering” result objects returned when using 2.0 style ORM queries, which would previously behave in “soft closed” style of returning empty results, or wouldn’t actually “soft close” at all and would continue yielding from the underlying cursor.
As part of this change, also added Result.close()
to the base Result
class and implemented it for the filtered result implementations that are used by the ORM, so that it is possible to call the CursorResult.close()
method on the underlying CursorResult
when the yield_per
execution option is in use to close a server side cursor before remaining ORM results have been fetched. This was again already available for Core result sets but the change makes it available for 2.0 style ORM results as well.
References: #7274
Query.filter_by()
would not function correctly on a Query
that was produced from Query.union()
, Query.from_self()
or similar.
References: #7239
load_only()
option were used to originally exclude that attribute, in the case where the load_only were descending from a relationship loader option. The fix allows that other valid options such as defer(..., raiseload=True)
etc. still function as expected.
References: #7304
Query.filter_by()
would not function correctly when Query.join()
were joined to an entity which made use of PropComparator.of_type()
to specify an aliased version of the target entity. The issue also applies to future style ORM queries constructed with select()
.
References: #7244
Connection
object where the Connection.execute()
method would not accept a non-dict mapping object, such as SQLAlchemy’s own RowMapping
or other abc.collections.Mapping
object as a parameter dictionary.
References: #7291
CursorResult.fetchmany()
method would fail to autoclose a server-side cursor (i.e. when stream_results
or yield_per
is in use, either Core or ORM oriented results) when the results were fully exhausted.
References: #7274
Engine
where calling upon Engine.begin()
and entering the context manager would not close the connection if the actual BEGIN operation failed for some reason, such as an event handler raising an exception; this use case failed to be tested for the future version of the engine. Note that the “future” context managers which handle begin()
blocks in Core and ORM don’t actually run the “BEGIN” operation until the context managers are actually entered. This is different from the legacy version which runs the “BEGIN” operation up front.
References: #7272
TupleType
to the top level sqlalchemy
import namespace.
Row
objects, would not be interpreted by the ColumnOperators.in_()
operator as tuple values to be broken out into individual bound parameters, and would instead pass them as single values to the driver leading to failures. The change to the “expanding IN” system now accommodates for the expression already being of type TupleType
and treats values accordingly if so. In the uncommon case of using “tuple-in” with an untyped statement such as a textual statement with no typing information, a tuple value is detected for values that implement collections.abc.Sequence
, but that are not str
or bytes
, as always when testing for Sequence
.
References: #7292
CTE
construct, when the CTE were embedded inside of an enclosing Select
statement that itself was set up as a scalar subquery.
References: #7269
text()
construct would no longer be accepted as a target case in the “whens” list within a case()
construct. The regression appears related to an attempt to guard against some forms of literal values that were considered to be ambiguous when passed here; however, there’s no reason the target cases shouldn’t be interpreted as open-ended SQL expressions just like anywhere else, and a literal string or tuple will be converted to a bound parameter as would be the case elsewhere.
References: #7287
Table
where the Table.implicit_returning
parameter would not be accommodated correctly when passed along with Table.extend_existing
to augment an existing Table
.
References: #7295
PGDialect_asyncpg.setup_asyncpg_json_codec
and PGDialect_asyncpg.setup_asyncpg_jsonb_codec
codec, which handle the required task of registering JSON/JSONB codecs for these datatypes when using asyncpg. The change is that methods are broken out as individual, overridable methods to support third party dialects that need to alter or disable how these particular codecs are set up.
References: #7284
Float
type to the “float” PostgreSQL type instead of “numeric” so that the value float(inf)
can be accommodated. Added test suite support for persistence of the “inf” value.
References: #7283
Insert.on_duplicate_key_update()
which would render the wrong column name when an expression were used in a VALUES expression. Pull request courtesy Cristian Sabaila.
References: #7281
Inspector.get_schema_names()
method when used in conjunction with the Connection.execution_options.schema_translate_map
feature, as well in the unlikely case that a symbol overlapping with the internal name “POSTCOMPILE” would be used with a feature like “expanding in”.
References: #7300
Released: October 19, 2021
Mapper.inherit_condition
parameter may be needed particularly for the ambiguous foreign keys case.
with_loader_criteria()
feature where ON criteria would not be added to a JOIN for a query of the form select(A).join(B)
, stating a target while making use of an implicit ON clause.
References: #7189
with_loader_criteria()
to work correctly, would not be applied to a select()
which queried from an ORM column expression if it made use of the ColumnElement.label()
modifier.
References: #7205
scoped_session
and async_scoped_session()
.
References: #7103
Query.join()
and the ORM version of Select.join()
, where a few places where “automatic aliasing” continues to occur will now be called out as a pattern to avoid, mostly specific to the area of joined table inheritance where classes that share common base tables are being joined together without using explicit aliases. One case emits a legacy warning for a pattern that’s not recommended, the other case is fully deprecated.
The automatic aliasing within ORM join() which occurs for overlapping mapped tables does not work consistently with all APIs such as contains_eager()
, and rather than continue to try to make these use cases work everywhere, replacing with a more user-explicit pattern is clearer, less prone to bugs and simplifies SQLAlchemy’s internals further.
The warnings include links to the errors.rst page where each pattern is demonstrated along with the recommended pattern to fix.
See also
An alias is being generated automatically for raw clauseelement
An alias is being generated automatically due to overlapping tables
References: #6972, #6974
Result
from a Session
after that Session
were closed would partially attach objects to that session in an essentially invalid state. It now raises an exception with a link to new documentation if an un-buffered result is iterated from a Session
that was closed or otherwise had the Session.expunge_all()
method called after that Result
was generated. The prebuffer_rows
execution option, as is used automatically by the asyncio extension for client-side result sets, may be used to produce a Result
where the ORM objects are prebuffered, and in this case iterating the result will produce a series of detached objects.
See also
Object cannot be converted to ‘persistent’ state, as this identity map is no longer valid.
References: #7128
"*"
were used in certain combinations, such as combining the joinedload()
loader strategy with raiseload('*')
of sub-elements.
References: #7134
hybrid_property
attribute or a mapped composite()
attribute as a key passed to the Update.values()
method for an ORM-enabled Update
statement, as well as when using it via the legacy Query.update()
method, would be processed for incoming ORM/hybrid/composite values within the compilation stage of the UPDATE statement, which meant that in those cases where caching occurred, subsequent invocations of the same statement would no longer receive the correct values. This would include not only hybrids that use the hybrid_property.update_expression()
method, but any use of a plain hybrid attribute as well. For composites, the issue instead caused a non-repeatable cache key to be generated, which would break caching and could fill up the statement cache with repeated statements.
The Update
construct now handles the processing of key/value pairs passed to Update.values()
and Update.ordered_values()
up front when the construct is first generated, before the cache key has been generated so that the key/value pairs are processed each time, and so that the cache key is generated against the individual column/value pairs that will ultimately be used in the statement.
References: #7209
Query
object to Session.execute()
is not the intended use of this object, and will now raise a deprecation warning.
References: #6284
Session.is_modified()
were made as well as the SessionEvents.do_orm_execute()
event hook were added.
References: #7169
URL
constructor which indicates that the URL.create()
method should be used would not emit if a full positional argument list of seven arguments were passed; additionally, validation of URL arguments will now occur if the constructor is called in this way, which was being skipped previously.
References: #7130
Inspector.reflect_table()
method now supports reflecting tables that do not have user defined columns. This allows MetaData.reflect()
to properly complete reflection on databases that contain such tables. Currently, only PostgreSQL is known to support such a construct among the common database backends.
References: #3247
__reduce__()
methods for all SQLAlchemy exception objects to ensure they all support clean round trips when pickling, as exception objects are often serialized for the purposes of various debugging tools.
References: #7077
FunctionElement.within_group()
construct could not be pickled, typically when using the sqlalchemy.ext.serializer
extension but also for general generic pickling.
References: #6520
HasCTE.cte.nesting
parameter introduced with #4123 where a recursive CTE
using HasCTE.cte.recursive
in typical conjunction with UNION would not compile correctly. Additionally makes some adjustments so that the CTE
construct creates a correct cache key. Pull request courtesy Eric Masseran.
References: #4123
table.schema
parameter passed to the table()
construct, such that it is taken into account when accessing the TableClause.fullname
attribute.
References: #7061
ColumnOperators.any_()
/ ColumnOperators.all_()
functions / methods where the special behavior these functions have of “flipping” the expression such that the “ANY” / “ALL” expression is always on the right side would not function if the comparison were against the None value, that is, “column.any_() == None” should produce the same SQL expression as “null() == column.any_()”. Added more docs to clarify this as well, plus mentions that any_() / all_() generally supersede the ARRAY version “any()” / “all()”.
References: #7140
TypeEngine.bind_expression()
method, where the method would need to be applied to each element of the IN expression rather than the overall IN expression itself.
References: #7177
null()
. This is based on the observation that at least some databases (e.g. MySQL, but not SQLite) will raise an error if the same label is repeated inside of a subquery.
References: #7153
Enum()
SQL types containing custom Python enumeration classes. Pull request courtesy Hiroshi Ogawa.
References: #6435
TypeEngine.bind_expression()
method with ARRAY
to portably apply the correct casts to elements. The asyncpg dialect was not affected by this issue as it applies bind-level casts at the driver level rather than at the compiler level.
References: #7177
match()
construct where passing a clause expression such as bindparam()
or other SQL expression for the “against” parameter would fail. Pull request courtesy Anton Kovalevich.
References: #7144
sqlalchemy.dialects.mysql
module would not be importable if “greenlet” were not installed.
References: #7204
Inspector.get_foreign_keys()
where foreign keys were omitted if they were established against a unique index instead of a unique constraint.
References: #7160
Inspector.has_table()
where it would return False if a local temp table with the same name from a different session happened to be returned first when querying tempdb. This is a continuation of #6910 which accounted for the temp table existing only in the alternate session and not the current one.
References: #7168
DATETIMEOFFSET
datatype where the ODBC implementation would not generate the correct DDL, for cases where the type were converted using the dialect.type_descriptor()
method, the usage of which is illustrated in some documented examples for TypeDecorator
, though not necessary for most datatypes. Regression was introduced by #6366. As part of this change, the full list of SQL Server date types have been amended to return a “dialect impl” that generates the same DDL name as the supertype.
References: #7129
Released: September 22, 2021
greenlet
dependency mis-spelled “aarch64” and additionally omitted uppercase “AMD64” as is needed for Windows machines. Pull request courtesy James Dow.
References: #7024
Released: September 22, 2021
platform_machine
to a specific identifier matches only the complete string.
References: #7024
Session.merge()
and AsyncSession.merge()
via a new Session.merge.options
parameter, which will apply the given loader options to the get()
used internally by merge, allowing eager loading of relationships etc. to be applied when the merge process loads a new object. Pull request courtesy Daniel Stone.
References: #6955
query()
or ORM-enabled select()
would be deduplicated on the identity of the object, such as a phrase like select(A.id, null(), null())
would produce only one “NULL” expression, which previously was not the case in 1.3. However, the change also allows for ORM expressions to render as given as well, such as select(A.data, A.data)
will produce a result row with two columns.
References: #6979
Query.with_entities()
method where the flag that determines automatic uniquing for legacy ORM Query
objects only would be set to True
inappropriately in cases where the with_entities()
call would be setting the Query
to return column-only rows, which are not uniqued.
References: #6924
_ConnectionFairy
object has two new attributes:
_ConnectionFairy.dbapi_connection
always represents a DBAPI compatible object. For pep-249 drivers, this is the DBAPI connection as it always has been, previously accessed under the .connection
attribute. For asyncio drivers that SQLAlchemy adapts into a pep-249 interface, the returned object will normally be a SQLAlchemy adaption object called AdaptedConnection
.
_ConnectionFairy.driver_connection
always represents the actual connection object maintained by the third party pep-249 DBAPI or async driver in use. For standard pep-249 DBAPIs, this will always be the same object as that of the dbapi_connection
. For an asyncio driver, it will be the underlying asyncio-only connection object.
The .connection
attribute remains available and is now a legacy alias of .dbapi_connection
.
See also
How do I get at the raw DBAPI connection when using an Engine?
References: #6832
Session.scalars()
, Connection.scalars()
, AsyncSession.scalars()
and AsyncSession.stream_scalars()
, which provide a short cut to the use case of receiving a row-oriented Result
object and converting it to a ScalarResult
object via the Result.scalars()
method, to return a list of values rather than a list of rows. The new methods are analogous to the long existing Session.scalar()
and Connection.scalar()
methods used to return a single value from the first row only. Pull request courtesy Miguel Grinberg.
References: #6990
ConnectionEvents.before_execute()
method to alter the SQL statement object passed, returning the new object to be invoked, was inadvertently removed. This behavior has been restored.
References: #6913
str()
is called on the an URL.create.password
argument, allowing usage of objects that implement the __str__()
method as password attributes. Also clarified that one such object is not appropriate to dynamically change the password for each database connection; the approaches at Generating dynamic authentication tokens should be used instead.
References: #6958
URL
where validation of “drivername” would not appropriately respond to the None
value where a string were expected.
References: #6983
create_engine.implicit_returning
set to False would fail to function when PostgreSQL’s “fast insertmany” feature were used in conjunction with a Sequence
, as well as if any kind of “executemany” with “return_defaults()” were used in conjunction with a Sequence
. Note that PostgreSQL “fast insertmany” uses “RETURNING” by definition, when the SQL statement is passed to the driver; overall, the create_engine.implicit_returning
flag is legacy and has no real use in modern SQLAlchemy, and will be deprecated in a separate change.
References: #6963
HasCTE.cte.nesting
to the CTE
constructor and HasCTE.cte()
method, which flags the CTE as one which should remain nested within an enclosing CTE, rather than being moved to the top level of the outermost SELECT. While in the vast majority of cases there is no difference in SQL functionality, users have identified various edge-cases where true nesting of CTE constructs is desirable. Much thanks to Eric Masseran for lots of work on this intricate feature.
References: #4123
FunctionElement
which, while unused, would lead pylint to report them as unimplemented abstract methods.
References: #7052
select()
and join()
when adapted to form a copy of the element would not completely copy the state of all column objects associated with subqueries. A key problem this caused is that usage of the ClauseElement.params()
method (which should probably be moved into a legacy category as it is inefficient and error prone) would leave copies of the old BindParameter
objects around, leading to issues in correctly setting the parameters at execution time.
References: #7055
HasCTE.add_cte()
feature where pairing two “INSERT…FROM SELECT” statements simultaneously would lose track of the two independent SELECT statements, leading to the wrong SQL.
References: #7036
Insert.values()
for “multi-valued insert” would not be processed correctly into the correct column expressions.
References: #7060
query_expression()
construct.
References: #6950
__tablename__
routine that came from a superclass.
References: #6937
asyncmy
asyncio database driver for MySQL and MariaDB. This driver is very new, however appears to be the only current alternative to the aiomysql
driver which currently appears to be unmaintained and is not working with current Python versions. Much thanks to long2ice for the pull request for this dialect.
See also
asyncmy
References: #6993
AsyncSession
now supports overriding which Session
it uses as the proxied instance. A custom Session
class can be passed using the AsyncSession.sync_session_class
parameter or by subclassing the AsyncSession
and specifying a custom AsyncSession.sync_session_class
.
References: #6746
AsyncSession.execute()
and AsyncSession.stream()
that required execution_options
to be an instance of immutabledict
when defined. It now correctly accepts any mapping.
References: #6943
**kw
arguments to the AsyncSession.connection()
method.
scoped_session
with asyncio drivers. When using Asyncio the async_scoped_session
should be used instead.
References: #6746
version()
call to avoid shadowing issues if a different search path is configured by the user.
References: #6912
ENUM
datatype is PostgreSQL-native and therefore should not be used with the native_enum=False
flag. This flag is now ignored if passed to the ENUM
datatype and a warning is emitted; previously the flag would cause the type object to fail to function correctly.
References: #6106
sqlalchemy.engine.reflection.has_table()
returned True
for local temporary tables that actually belonged to a different SQL Server session (connection). An extra check is now performed to ensure that the temp table detected is in fact owned by the current session.
References: #6910
Released: August 18, 2021
greenlet
is a default requirement only for those platforms that are well known for greenlet
to be installable and for which there is already a pre-built binary on pypi; the current list is x86_64 aarch64 ppc64le amd64 win32
. For other platforms, greenlet will not install by default, which should enable installation and test suite running of SQLAlchemy 1.4 on platforms that don’t support greenlet
, excluding any asyncio features. In order to install with the greenlet
dependency included on a machine architecture outside of the above list, the [asyncio]
extra may be included by running pip install sqlalchemy[asyncio]
which will then attempt to install greenlet
.
Additionally, the test suite has been repaired so that tests can complete fully when greenlet is not installed, with appropriate skips for asyncio-related tests.
References: #6136
Select.columns_clause_froms
that will retrieve the FROM list implied by the columns clause of the Select
statement. This differs from the old Select.froms
collection in that it does not perform any ORM compilation steps, which necessarily deannotate the FROM elements and do things like compute joinedloads etc., which makes it not an appropriate candidate for the Select.select_from()
method. Additionally adds a new parameter Select.with_only_columns.maintain_column_froms
that transfers this collection to Select.select_from()
before replacing the columns collection.
In addition, the Select.froms
is renamed to Select.get_final_froms()
, to stress that this collection is not a simple accessor and is instead calculated given the full state of the object, which can be an expensive call when used in an ORM context.
Additionally fixes a regression involving the with_only_columns()
function to support applying criteria to column elements that were replaced with either Select.with_only_columns()
or Query.with_entities()
, which had broken as part of #6503 released in 1.4.19.
References: #6808
Load.options()
method, particularly when nesting multiple calls, would generate an overly long and more importantly non-deterministic cache key, leading to very large cache keys which were also not allowing efficient cache usage, both in terms of total memory used as well as number of entries used in the cache itself.
References: #6869
ORMExecuteState.user_defined_options
accessor receives UserDefinedOption
and related option objects from the context, with particular emphasis on the “selectinload” on the loader strategy where this previously was not working; other strategies did not have this problem. The objects that are associated with the current query being executed, and not that of a query being cached, are now propagated unconditionally. This essentially separates them out from the “loader strategy” options which are explicitly associated with the compiled state of a query and need to be used in relation to the cached query.
The effect of this fix is that a user-defined option, such as those used by the dogpile.caching example as well as for other recipes such as defining a “shard id” for the horizontal sharing extension, will be correctly propagated to eager and lazy loaders regardless of whether a cached query was ultimately invoked.
References: #6887
selectinload()
where use of the new PropComparator.and_()
feature within options that were nested more than one level deep would fail to update bound parameter values that were in the nested criteria, as a side effect of SQL statement caching.
References: #6881
Bundle
construct would not create proper cache keys, leading to inefficient use of the query cache. This had some impact on the “selectinload” strategy and was identified as part of #6889.
References: #6889
CTE
where new HasCTE.add_cte()
method added in version 1.4.21 / #6752 failed to function correctly for “compound select” structures such as union()
, union_all()
, except()
, etc. Pull request courtesy Eric Masseran.
References: #6752
CacheKey.to_offline_string()
method used by the dogpile.caching example where attempting to create a proper cache key from the special “lambda” query generated by the lazy loader would fail to include the parameter values, leading to an incorrect cache key.
References: #6858
Enum
in native and non-native implementations regarding the accepted values for an enum with aliased elements. When Enum.omit_aliases
is False
all values, alias included, are accepted as valid values. When Enum.omit_aliases
is True
only non aliased values are accepted as valid values.
References: #6146
sqlalchemy2-stubs
, e.g. Column[String]
, without the need for qualifying these constructs within a TYPE_CHECKING
block by implementing the Python special method __class_getitem__()
, which allows this syntax to pass without error at runtime.
References: #6759, #6804
literal_binds
compiler flag, as used externally to render bound parameters inline, would fail to work when used with a certain class of parameters known as “literal_execute”, which covers things like LIMIT and OFFSET values for dialects where the drivers don’t allow a bound parameter, such as SQL Server’s “TOP” clause. The issue locally seemed to affect only the MSSQL dialect.
References: #6863
Session.execute()
.
References: #6816
Released: July 21, 2021
Table.table_valued()
method where the resulting TableValuedColumn
construct would not respond correctly to alias adaptation as is used throughout the ORM, such as for eager loading, polymorphic loading, etc.
References: #6775
Result.unique()
method with an ORM result that included column expressions with unhashable types, such as JSON
or ARRAY
using non-tuples would silently fall back to using the id()
function, rather than raising an error. This now raises an error when the Result.unique()
method is used in a 2.0 style ORM query. Additionally, hashability is assumed to be True for result values of unknown type, such as often happens when using SQL functions of unknown return type; if values are truly not hashable then the hash()
itself will raise.
For legacy ORM queries, since the legacy Query
object uniquifies in all cases, the old rules remain in place, which is to use id()
for result values of unknown type as this legacy uniquing is mostly for the purpose of uniquing ORM entities and not column values.
References: #6769
list()
has been applied to prevent concurrent GC from affecting this operation.
References: #6771
KeyError
in the event system to accommodate the case that the interpreter is shutting down at the same time Engine.dispose()
is being called, which would cause stack trace warnings.
References: #6740
case.whens
parameter passing a dictionary positionally and not as a keyword argument would emit a 2.0 deprecation warning, referring to the deprecation of passing a list positionally. The dictionary format of “whens”, passed positionally, is still supported and was accidentally marked as deprecated.
References: #6786
Insert.values()
method with the Python None
value; in particular, this would be noticed when using the JSON
datatype as well as related PostgreSQL specific types such as JSONB
which would fail to encode the Python None
value into JSON null, however the issue was generalized to any bound parameter handler in conjunction with this specific method of Insert
.
References: #6770
Released: July 14, 2021
AttributeEvents.set()
event, which is nonetheless currently documented, which is that the event applied to such a one-to-one attribute will no longer receive the “old” parameter if it is unloaded and the relationship.active_history
flag is not set. As is documented in AttributeEvents.set()
, if the event handler needs to receive the “old” value when the event fires off, the active_history flag must be established either with the event listener or with the relationship. This is already the behavior with other kinds of attributes such as many-to-one and column value references.
The change additionally will defer updating a backref on the “old” value in the less common case that the “old” value is locally present in the session, but isn’t loaded on the relationship in question, until the next flush occurs. If this causes an issue, again the normal relationship.active_history
flag can be set to True
on the relationship.
References: #6708
Query.with_entities()
where the new structure used would be inappropriately transferred to an enclosing Query
when making use of set operations such as Query.union()
, causing the JOIN instructions within to be applied to the outside query as well.
References: #6698
with_polymorphic()
against a mapping which uses a column_property()
which in turn makes use of a scalar select that includes a aliased()
object of the mapped table.
References: #6762
HasCTE.add_cte()
to each of the select()
, insert()
, update()
and delete()
constructs. This method will add the given CTE
as an “independent” CTE of the statement, meaning it renders in the WITH clause above the statement unconditionally even if it is not otherwise referenced in the primary statement. This is a popular use case on the PostgreSQL database where a CTE is used for a DML statement that runs against database rows independently of the primary statement.
References: #6752
tablesample()
construct would fail to be executable when constructed given a floating-point sampling value not embedded within a SQL function.
References: #6735
Insert.on_conflict_do_nothing()
and Insert.on_conflict_do_update()
where the name of a unique constraint passed as the constraint
parameter would not be properly truncated for length if it were based on a naming convention that generated a too-long name for the PostgreSQL max identifier length of 63 characters, in the same way which occurs within a CREATE TABLE statement.
References: #6755
ENUM
datatype as embedded in the ARRAY
datatype would fail to emit correctly in create/drop when the schema_translate_map
feature were also in use. Additionally repairs a related issue where the same schema_translate_map
feature would not work for the ENUM
datatype in combination with a CAST
, that’s also intrinsic to how the ARRAY(ENUM)
combination works on the PostgreSQL dialect.
References: #6739
Insert.on_conflict_do_nothing()
and Insert.on_conflict_do_update()
where the name of a unique constraint passed as the constraint
parameter would not be properly quoted if it contained characters which required quoting.
References: #6696
schema_translate_map
feature.
References: #6697
Released: June 28, 2021
with_polymorphic()
construct, when the user-facing object is garbage collected as the query is processed. The reconstitution was not ensuring the sub-entities for the “polymorphic” case were handled, leading to an AttributeError
.
References: #6680
Query.union()
and similar set operations to be correctly compatible with the new capabilities just added in #6661, with SQLAlchemy 1.4.19, such that the SELECT statements rendered as elements of the UNION or other set operation will include directly mapped columns that are mapped as deferred; this both fixes a regression involving unions with multiple levels of nesting that would produce a column mismatch, and also allows the undefer()
option to be used at the top level of such a Query
without having to apply the option to each of the elements within the UNION.
References: #6678
@validates
validator function or a @reconstructor
reconstruction function, to check for “callable” more liberally such as to accommodate objects based on fundamental attributes like __func__
and __call__
, rather than testing for MethodType
/ FunctionType
, allowing things like cython functions to work properly. Pull request courtesy Miłosz Stypiński.
References: #6538
Row
class which could lead to a memory leak in the unlikely case of a Row
object which referred to an ORM object that then was mutated to refer back to the Row
itself, creating a cycle. The Python C APIs for tracking GC cycles has been added to the native Row
implementation to accommodate for this case.
References: #5348
select()
made against the token “*”, which then yielded exactly one column, would fail to correctly organize the cursor.description
column name into the keys of the result object.
References: #6665
PickleType
constructor, allowing any arbitrary type to be used in place of the default implementation of LargeBinary
. Pull request courtesy jason3gb.
References: #6646
Sequence
and the more general DefaultGenerator
base, as these are “executable” as statements they need to include Executable
in their hierarchy, not just StatementRole
as was applied arbitrarily to Sequence
previously. The fix allows Sequence
to work in all .execute()
methods including with Session.execute()
which was not working in the case that a SessionEvents.do_orm_execute()
handler was also established.
References: #6668
None
for the value of Table.prefixes
would not store an empty list, but rather the constant None
, which may be unexpected by third party dialects. The issue is revealed by a usage in recent versions of Alembic that are passing None
for this value. Pull request courtesy Kai Mueller.
References: #6685
sqlalchemy.ext.automap
extension such that the use case of creating an explicit mapped class to a table that is also the relationship.secondary
element of a relationship()
that automap will be generating would emit the “overlaps” warnings introduced in 1.4 and discussed at relationship X will copy column Q to column P, which conflicts with relationship(s): ‘Y’. While generating this case from automap is still subject to the same caveats mentioned in the ‘overlaps’ warning, since automap is primarily intended for more ad-hoc use cases, the condition triggering the warning is disabled when a many-to-many relationship with this specific pattern is generated.
References: #6679
Released: June 22, 2021
Query.join()
would fail if the left side of the statement for which the option/join depends upon were replaced by using the Query.with_entities()
method, or when using 2.0 style queries when using the Select.with_only_columns()
method. A new set of state has been added to the objects which tracks the “left” entities that the options / join were made against which is memoized when the lead entities are changed.
References: #6253, #6503
undefer()
, a subquery that is against an ORM entity with deferred attributes will now render those deferred attributes that refer directly to mapped table columns, as these are needed in the outer SELECT if that outer SELECT makes use of these columns; however a deferred attribute that refers to a composed SQL expression as we normally do with column_property()
will not be part of the subquery, as these can be selected explicitly if needed in the subquery. If the entity is being SELECTed from this subquery, the column expression can still render on “the outside” in terms of the derived subquery columns. This produces essentially the same behavior as when working with 1.3. However in this case the fix has to also make sure that the .selected_columns
collection of an ORM-enabled select()
also follows these rules, which in particular allows recursive CTEs to render correctly in this scenario, which were previously failing to render correctly due to this issue.
References: #6661
column_property()
mappings would render in the WITH RECURSIVE xyz(...)
section as their raw internal label and not a cleanly anonymized name.
References: #6663
async_scoped_session
to address some asyncio-related incompatibilities between scoped_session
and AsyncSession
, in which some methods (notably the async_scoped_session.remove()
method) should be used with the await
keyword.
See also
Using asyncio scoped session
References: #6583
BaseException
subclasses, most notably including asyncio.CancelledError
, to the exception handling logic used by the engine to invalidate and clean up the connection, thus preventing connections from being correctly disposed when a task was cancelled.
References: #6652
INTERVAL
datatype on PostgreSQL and Oracle would produce an AttributeError
when used in the context of a comparison operation against a timedelta()
object. Pull request courtesy MajorDallas.
References: #6649
match
, which provides for the full range of MySQL’s MATCH operator including multiple column support and modifiers. Pull request courtesy Anton Kovalevich.
See also
match
References: #6132
Released: June 10, 2021
relationship.bake_queries
flag, which in 1.4 is to enable or disable “lambda caching” of statements within the “lazyload” and “selectinload” loader strategies; this is separate from the more foundational SQL query cache that is used for most statements. Additionally, the lazy loader no longer uses its own cache for many-to-one SQL queries, which was an implementation quirk that doesn’t exist for any other loader scenario. Finally, the “lru cache” warning that the lazyloader and selectinloader strategies could emit when handling a wide array of class/relationship combinations has been removed; based on analysis of some end-user cases, this warning doesn’t suggest any significant issue. While setting bake_queries=False
for such a relationship will remove this cache from being used, there’s no particular performance gain in this case as using no caching vs. using a cache that needs to refresh often likely still wins out on the caching being used side.
References: #6072, #6487
scoped_session
and AsyncSession
are generated from the base Session
class, such that custom Session
subclasses such as that used by Flask-SQLAlchemy don’t need to implement positional arguments when they call into the superclass method, and can continue using the same argument styles as in previous releases.
References: #6285
relationship()
when multiple relationships would overlap with each other as far as foreign key attributes written towards, now includes the specific “overlaps” argument to use for each warning in order to silence the warning without changing the mapping.
References: #6400
Async
version of an object, like AsyncSession
, AsyncConnection
, etc., to be locatable given the proxied “sync” object, i.e. Session
, Connection
. Previously, to the degree such lookup functions were used, an Async
object would be re-created each time, which was less than ideal as the identity and state of the “async” object would not be preserved across calls.
From there, new helper functions async_object_session()
, async_session()
as well as a new InstanceState
attribute InstanceState.async_session
have been added, which are used to retrieve the original AsyncSession
associated with an ORM mapped object, a Session
associated with an AsyncSession
, and an AsyncSession
associated with an InstanceState
, respectively.
This patch also implements new methods AsyncSession.in_nested_transaction()
, AsyncSession.get_transaction()
, AsyncSession.get_nested_transaction()
.
References: #6319
NullPool
or the StaticPool
with an async engine. This mostly affected the aiosqlite dialect.
References: #6575
asyncio.exceptions.TimeoutError
, asyncio.exceptions.CancelledError
as so-called “exit exceptions”, a class of exceptions that include things like GreenletExit
and KeyboardInterrupt
, which are considered to be events that warrant considering a DBAPI connection to be in an unusable state where it should be recycled.
References: #6592
on_connect_url
hook was erroneously not receiving a URL
object under normal usage of create_engine()
and instead received a string that was unhandled; the test suite failed to fully set up the actual conditions under which this hook is called. This has been fixed.
References: #6586
Released: May 29, 2021
Released: May 28, 2021
relationship.cascade_backrefs
parameter set to False
, which per cascade_backrefs behavior deprecated for removal in 2.0 is set to become the standard behavior in SQLAlchemy 2.0, where adding the item to a collection that uniquifies, such as set
or dict
would fail to fire a cascade event if the object were already associated in that collection via the backref. This fix represents a fundamental change in the collection mechanics by introducing a new event state which can fire off for a collection mutation even if there is no net change on the collection; the action is now suited using a new event hook AttributeEvents.append_wo_mutation()
.
References: #6471
Select.join()
given an ORM attribute as a target.
References: #6550
Session.execute()
so that when a non-ORM statement such as an insert()
construct nonetheless is built against ORM objects, to the greatest degree possible the ORM entity will be used to resolve the bind, such as for a Session
that has a bind map set up on a common superclass without specific mappers or tables named in the map.
References: #6484
@
sign in the database portion of a URL would not be interpreted correctly if the URL also had a username:password section.
References: #6482
URL
where query parameters following the question mark would not be parsed correctly if the URL did not contain a database portion with a backslash.
References: #6329
relationship()
overall where the relationship.order_by
parameter were stored as a mutable list, which could then be mutated when combined with additional “order_by” methods used against the dynamic query object, causing the ORDER BY criteria to continue to grow repetitively.
References: #6549
CTE
construct to be used directly as the target of a delete()
construct, i.e. “WITH … AS cte DELETE FROM cte”. This appears to be a useful feature of SQL Server.
References: #6464
automap_base()
without passing an existing Base
.
References: #6529
sqlalchemy.ext.instrumentation
extension that prevented instrumentation disposal from working completely. This fix includes both a 1.4 regression fix as well as a fix for a related issue that existed in 1.3 also. As part of this change, the sqlalchemy.ext.instrumentation.InstrumentationManager
class now has a new method unregister()
, which replaces the previous method dispose()
, which was not called as of version 1.4.
References: #6390
Released: May 11, 2021
contains_eager()
eagerload option in the case that the contains_eager()
were chained to an additional eager loader option, which would then produce an incorrect query as the original query-bound join criteria were no longer present.
References: #6449
BindParameter
object occurs within the clause construction phase as it did before, rather than in the compilation phase. In the latter case, the mutation still produced side effects against the incoming construct and additionally could potentially interfere with other internal mutation routines.
References: #6460
ssl_check_hostname=
parameter in mysql connection URIs and updated the mysql dialect documentation regarding secure connections. Original pull request courtesy of Jerry Zhao.
References: #5397
Released: May 6, 2021
lazy='dynamic'
loader in conjunction with a detached object. The previous behavior was that the dynamic loader upon calling methods like .all()
returns empty lists for detached objects without error, this has been restored; however a warning is now emitted as this is not the correct result. Other dynamic loader scenarios correctly raise DetachedInstanceError
.
References: #6426
.commit()
or .rollback()
inside of an existing .begin()
context manager, with the addition of potentially emitting SQL within the block subsequent to the commit or rollback. This change continues upon the change first added in #6155 where the use case of calling “rollback” inside of a .begin()
contextmanager block was proposed:
.commit()
or .rollback()
will now be allowed without error or warning within all scopes, including that of legacy and future Engine
, ORM Session
, asyncio AsyncEngine
. Previously, the Session
disallowed this.
.commit()
or .rollback()
is called. The block should be closed as the state of the executable object would otherwise be undefined in this state.
References: #6288
CursorResult.keys()
method for a statement that returns no rows to provide support for legacy patterns used by the “records” package as well as any other non-migrated applications. Previously, this would raise ResourceClosedException
unconditionally in the same way as it does when attempting to fetch rows. While this is the correct behavior going forward, the LegacyCursorResult
object will now in this case return an empty list for .keys()
as it did in 1.3, while also emitting a 2.0 deprecation warning. The _cursor.CursorResult
, used when using a 2.0-style “future” engine, will continue to raise as it does now.
References: #6427
TypeDecorator
class will now emit a warning when used in SQL compilation with caching unless the .cache_ok
flag is set to True
or False
. A new class-level attribute TypeDecorator.cache_ok
may be set which will be used as an indication that all the parameters passed to the object are safe to be used as a cache key if set to True
, False
means they are not.
References: #6436
Released: May 3, 2021
selectinload
loader strategy that would cause it to cache its internal state incorrectly when handling relationships that join across more than one column, such as when using a composite foreign key. The invalid caching would then cause other unrelated loader operations to fail.
References: #6410
Query.filter_by()
would not work if the lead entity were a SQL function or other expression derived from the primary entity in question, rather than a simple entity or column of that entity. Additionally, improved the behavior of Select.filter_by()
overall to work with column expressions even in a non-ORM context.
References: #6414
selectinload()
and subqueryload()
to load a two-level-deep path would lead to an attribute error.
References: #6419
noload()
loader strategy in conjunction with a “dynamic” relationship would lead to an attribute error as the noload strategy would attempt to apply itself to the dynamic loader.
References: #6420
Connection
as it was never tested as a known use case in previous versions, where calling upon the Connection.begin_nested()
method, when no transaction is present, does not create a SAVEPOINT at all and instead starts an outer transaction, returning a RootTransaction
object instead of a NestedTransaction
object. This RootTransaction
then will emit a real COMMIT on the database connection when committed. Previously, the 2.0 style behavior was present in all cases that would autobegin a transaction but not commit it, which is a behavioral change.
When using a 2.0 style connection object, the behavior is unchanged from previous 1.4 versions; calling Connection.begin_nested()
will “autobegin” the outer transaction if not already present, and then as instructed emit a SAVEPOINT, returning the NestedTransaction
object. The outer transaction is committed by calling upon Connection.commit()
, as is “commit-as-you-go” style usage.
In non-“future” mode, while the old behavior is restored, it also emits a 2.0 deprecation warning as this is a legacy behavior.
References: #6408
asyncio.Lock
which could be attached to the wrong loop when instantiating the async engine before any asyncio loop was started, leading to an asyncio error message when attempting to use the engine under certain circumstances.
References: #6409
Connection.execution_options.stream_results
option.
References: #6198
Released: April 29, 2021
Session.bulk_save_objects()
when used with persistent objects which would fail to track the primary key of mappings where the column name of the primary key were different than the attribute name.
This change is also backported to: 1.3.25
References: #6392
hybrid_property
, which would come into play under common mis-configuration scenarios that were silently ignored in 1.3, and now failed in 1.4, where the “expression” implementation would return a non ClauseElement
such as a boolean value. For both issues, 1.3’s behavior was to silently ignore the mis-configuration and ultimately attempt to interpret the value as a SQL expression, which would lead to an incorrect query.
__clause_element__()
method of the attribute returned a non-ClauseElement
object, an internal AttributeError
would lead the attribute to return the expression
function on the hybrid_property itself, as the attribute error was against the name .expression
which would invoke the __getattr__()
method as a fallback. This now raises explicitly. In 1.3 the non-ClauseElement
was returned directly.
References: #6350
Select
as a subquery in an ORM context would modify the Select
in place to disable eagerloads on that object, which would then cause that same Select
to not eagerload if it were then re-used in a top-level execution context.
References: #6378
Session.rollback()
in that no snapshot was created to be rolled back. The “attribute modify” mechanics have been updated to ensure “autobegin”, which does not perform any database work, does occur when persistent attributes change in the same manner as when Session.add()
is called. This is a regression as in 1.3, the rollback() method always had a transaction to roll back and would expire every time.
References: #6359, #6360
AttributeError
when attempting to generate an entry for the .c
collection of a subquery in some cases; among other things this would impact its use in cases like that of Query.count()
.
References: #6401
declared_attr()
established on a mixin, when using the new form of having it inside of a dataclasses.field()
construct and not actually a descriptor attribute on the class, correctly accommodates the case when the target class to be mapped is a subclass of an existing mapped class which has already mapped that declared_attr()
, and therefore should not be re-applied to this class.
References: #6346
ForeignKeyConstraint.copy()
method that caused an error when invoked with the schema
argument.
References: #6353
Sequence
would produce inconsistent “inline” behavior for an Insert
construct that includes multiple values phrases; the first seq would be inline but subsequent ones would be “pre-execute”, leading to inconsistent sequence ordering. The sequence expressions are now fully inline.
References: #6361
def visit_empty_set_op_expr(self, type_, expand_op)
, which takes precedence over the existing def visit_empty_set_expr(self, element_types)
which remains in place.
References: #6258, #6397
text()
construct inside the columns clause of a Select
construct, which is better handled by using a literal_column()
construct, would nonetheless prevent constructs like union()
from working correctly. Other use cases, such as constructing subuqeries, continue to work the same as in prior versions where the text()
construct is silently omitted from the collection of exported columns. Also repairs similar use within the ORM.
References: #6343
Select.append_column()
where internal assertions would fail.
References: #6261
select()
now caused failures when doing 2.0-style querying with a mapped class that also happens to have an __iter__()
method. Tuned the check some more to accommodate this as well as some other interesting __iter__()
scenarios.
References: #6300
Identity
construct while rendering the AUTO_INCREMENT
keyword in a create table.
The Oracle and PostgreSQL compiler was updated to not render Identity
if the database version does not support it (Oracle < 12 and PostgreSQL < 10). Previously it was rendered regardless of the database version.
References: #6338
Enum
datatype would not inherit the MetaData.schema
parameter of a MetaData
object when that object were passed to the Enum
using Enum.metadata
.
References: #6373
SingletonThreadPool
for in-memory SQLite databases created using URI filenames. Previously the default pool used was the NullPool
that precented sharing the same database between multiple engines.
References: #6379
TypeEngine.as_generic()
support for sqlalchemy.dialects.mysql.BIT
columns, mapping them to Boolean
.
References: #6345
DateTime(timezone=True)
, where the previous behavior of the pyodbc driver of implicitly dropping the tzinfo from a timezone-aware date when INSERTing into a timezone-naive DATETIME column were lost, leading to a SQL Server error when inserting timezone-aware datetime objects into timezone-native database columns.
References: #6366
Released: April 21, 2021
__new__()
method.
References: #6331
Released: April 20, 2021
immediateload
loader strategy no longer goes into recursive loops; the modification is that an eager load (joinedload, selectinload, or subqueryload) from A->bs->B which then states immediateload
for a simple manytoone B->a->A that’s in the identity map will populate the B->A, so that this attribute is back-populated when the collection of A/A.bs are loaded. This allows the objects to be functional when detached.
with_loader_criteria()
feature where using a mixin class with declared_attr()
on an attribute that were accessed inside the custom lambda would emit a warning regarding using an unmapped declared attr, when the lambda callable were first initialized. This warning is now prevented using special instrumentation for this lambda initialization step.
References: #6320
populate_existing
, which given the new feature now overwrites pending changes on eagerly loaded objects when autoflush is false. The populate_existing flag has been turned off for this case and a more specific method used to ensure the correct attributes refreshed.
References: #6326
Result.scalar_one()
or Result.scalar_one_or_none()
after calling Result.unique()
, for the case where the ORM is returning a single-element row in any case.
References: #6299
Values
construct wouldn’t be positionally tracked correctly if inside of a CTE
, affecting database drivers that support VALUES + ctes and use positional parameters such as SQL Server in particular as well as asyncpg. The fix also repairs support for compiler flags such as literal_binds
.
References: #6327
str(obj)
to get a string representation to use as an anonymous column name in the .c
collection of a subquery. This is a very legacy behavior that performs poorly and leads to lots of issues, so has been revised to no longer perform any compilation by establishing specific methods on FunctionElement
to handle this case, as SQL functions are the only use case that it came into play. An effect of this behavior is that an unlabeled column expression with no derivable name will be given an arbitrary label starting with the prefix "_no_label"
in the .c
collection of a subquery; these were previously being represented either as the generic stringification of that expression, or as an internal symbol.
References: #6256
next_value()
was not deriving its type from the corresponding Sequence
, instead hardcoded to Integer
. The specific numeric type is now used.
References: #6287
Mapped
annotation in conjunction with a relationship()
that refers to a class by string name; the correct annotation would be downgraded to a less specific one leading to typing errors.
References: #6255
DateTime.timezone
parameter when set to True
will now make use of the DATETIMEOFFSET
column type with SQL Server when used to emit DDL, rather than DATETIME
where the flag was silently ignored.
References: #6306
instrument_declarative()
that called a non existing registry method.
References: #6291
Released: April 17, 2021
synoynm()
in conjunction with hybrid property, assocaitionproxy is set up completely, including that synonyms can be established linking to these constructs which work fully. This is a behavior that was semi-explicitly disallowed previously, however since it did not fail in every scenario, explicit support for assoc proxy and hybrids has been added.
References: #6267
select()
construct would traverse a repetitive product of the represented FROM clauses as they were each referenced by columns in the columns clause; for a series of nested subqueries with lots of columns this could cause a large delay and significant memory growth. This traversal is used by a wide variety of SQL and ORM functions, including by the ORM Session
when it’s configured to have “table-per-bind”, which while this is not a common use case, it seems to be what Flask-SQLAlchemy is hardcoded as using, so the issue impacts Flask-SQLAlchemy users. The traversal has been repaired to uniqify on FROM clauses which was effectively what would happen implicitly with the pre-1.4 architecture.
References: #6304
synonym()
could not be used in column loader options such as load_only()
.
References: #6272
literal_binds=True
.
References: #6290
Released: April 15, 2021
with_expression()
loader option, where the given SQL expression would not be correctly considered as part of the cache key.
Additionally, fixed regression involving the corresponding query_expression()
feature. While the bug technically exists in 1.3 as well, it was not exposed until 1.4. The “default expr” value of null()
would be rendered when not needed, and additionally was also not adapted correctly when the ORM rewrites statements such as when using joined eager loading. The fix ensures “singleton” expressions like NULL
and true
aren’t “adapted” to refer to columns in ORM statements, and additionally ensures that a query_expression()
with no default expression doesn’t render in the statement if a with_expression()
isn’t used.
References: #6259
Session.refresh()
introduced by #1763 where eagerly loaded relationships are also refreshed, where the lazy="raise"
and lazy="raise_on_sql"
loader strategies would interfere with the immediateload()
loader strategy, thus breaking the feature for relationships that were loaded with selectinload()
, subqueryload()
as well.
References: #6252
Dialect.has_table()
method now raises an informative exception if a non-Connection is passed to it, as this incorrect behavior seems to be common. This method is not intended for external use outside of a dialect. Please use the Inspector.has_table()
method or for cross-compatibility with older SQLAlchemy versions, the Engine.has_table()
method.
CursorResult.inserted_primary_key
is now a Row
object with a named tuple interface on top of the existing tuple interface.
References: #3314
BindParameter
object would not properly render for an IN expression (i.e. using the “post compile” feature in 1.4) if the object were copied from either an internal cloning operation, or from a pickle operation, and the parameter name contained spaces or other special characters.
References: #6249
OrderingList
from version 1.4.7 which was testing against the incorrect API.
References: #6205
bind
attribute of an AsyncSession
to the correct value.
References: #6220
Select.offset()
is not used. Note that this change now requires that if the “with_ties” or “percent” modifiers are used, the statement can’t specify an OFFSET of zero, it now needs to be omitted entirely.
References: #6265
Released: April 9, 2021
subqueryload()
loader strategy would fail to correctly accommodate sub-options, such as a defer()
option on a column, if the “path” of the subqueryload were more than one level deep.
References: #6221
merge_frozen_result()
function relied upon by the dogpile.caching example was not included in tests and began failing due to incorrect internal arguments.
References: #6211
Session
could fail to “autobegin” a new transaction when a flush occurred without an existing transaction in place, implicitly placing the Session
into legacy autocommit mode which commit the transaction. The Session
now has a check that will prevent this condition from occurring, in addition to repairing the flush issue.
Additionally, scaled back part of the change made as part of #5226 which can run autoflush during an unexpire operation, to not actually do this in the case of a Session
using legacy Session.autocommit
mode, as this incurs a commit within a refresh operation.
References: #6233
AttributeError
would be raised, when it would attempt to determine the correct name for each element in a result tuple. A similar issue exists in 1.3 but only impacts the names of tuple rows. The fix here adds a check that the hybrid’s function name is actually present in the __dict__
of the class or its superclasses before assigning this name; otherwise, the hybrid is considered to be “unnamed” and ORM result tuples will use the naming scheme of the underlying expression.
References: #6215
Row
object when dictionary access is used upon it, meaning converting to a dict via dict(row)
or accessing members using strings or other objects i.e. row["some_key"]
works as it would with a dictionary, rather than raising TypeError
as would be the case with a tuple, whether or not the C extensions are in place. This was originally supposed to emit a 2.0 deprecation warning for the “non-future” case using LegacyRow
, and was to raise TypeError
for the “future” Row
class. However, the C version of Row
was failing to raise this TypeError
, and to complicate matters, the Session.execute()
method now returns Row
in all cases to maintain consistency with the ORM result case, so users who didn’t have C extensions installed would see different behavior in this one case for existing pre-1.4 style code.
Therefore, in order to soften the overall upgrade scheme as most users have not been exposed to the more strict behavior of Row
up through 1.4.6, LegacyRow
and Row
both provide for string-key access as well as support for dict(row)
, in all cases emitting the 2.0 deprecation warning when SQLALCHEMY_WARN_20
is enabled. The Row
object still uses tuple-like behavior for __contains__
, which is probably the only noticeable behavioral change compared to LegacyRow
, other than the removal of dictionary-style methods values()
and items()
.
References: #6218
ColumnOperators.in_()
operations to infer the type of expression from the right hand list of elements, if the left hand side does not have any explicit type set up. This allows the expression to support stringification among other things. In 1.3, “expanding” was not automatically used for ColumnOperators.in_()
expressions, so in that sense this change fixes a behavioral regression.
References: #6222
Connection.execution_options.schema_translate_map
dictionary which contained special characters such as braces would fail to be substituted properly. Use of square bracket characters []
is now explicitly disallowed as these are used as a delimiter character in the current implementation.
References: #6216
TypeEngine
, in particular that of TypeDecorator
and UserDefinedType
.
DefaultDialect
called supports_schemas
; third party dialects may set this flag to False
to disable SQLAlchemy’s schema-level tests when running the test suite for a third party dialect.
Released: April 6, 2021
Query.join()
were used, passing a series of entities to join from without any ON clause in a single Query.join()
call, would fail to function correctly.
References: #6203
Query.yield_per()
method in the ORM would set up the internal Result
to yield chunks at a time, however made use of the new Result.unique()
method which uniques across the entire result. This would lead to lost rows since the ORM is using id(obj)
as the uniquing function, which leads to repeated identifiers for new objects as already-seen objects are garbage collected. 1.3’s behavior here was to “unique” across each chunk, which does not actually produce “uniqued” results when results are yielded in chunks. As the Query.yield_per()
method is already explicitly disallowed when joined eager loading is in place, which is the primary rationale for the “uniquing” feature, the “uniquing” feature is now turned off entirely when Query.yield_per()
is used.
This regression only applies to the legacy Query
object; when using 2.0 style execution, “uniquing” is not automatically applied. To prevent the issue from arising from explicit use of Result.unique()
, an error is now raised if rows are fetched from a “uniqued” ORM-level Result
if any yield per API is also in use, as the purpose of yield_per
is to allow for arbitrarily large numbers of rows, which cannot be uniqued in memory without growing the number of entries to fit the complete result size.
References: #6206
Subquery
using Connection.execute()
is deprecated and will emit a deprecation warning; this use case was an oversight that should have been removed from 1.4. The operation will now execute the underlying Select
object directly for backwards compatibility. Similarly, the CTE
class is also not appropriate for execution. In 1.3, attempting to execute a CTE would result in an invalid “blank” SQL statement being executed; since this use case was not working it now raises ObjectNotExecutableError
. Previously, 1.4 was attempting to execute the CTE as a statement however it was working only erratically.
References: #6204
Table
object now raises an informative error message if it is instantiated without passing at least the Table.name
and Table.metadata
arguments positionally. Previously, if these were passed as keyword arguments, the object would silently fail to initialize correctly.
This change is also backported to: 1.3.25
References: #6135
declarative_mixin()
is added, which is necessary for the Mypy plugin to be able to definifitely identify a Declarative mixin class that is otherwise not used inside a particular Python file.
See also
Using @declared_attr and Declarative Mixins
References: #6147
.sqlstate
and synonym .pgcode
to the .orig
attribute of the SQLAlchemy exception class raised by the asyncpg DBAPI adapter, that is, the intermediary exception object that wraps on top of that raised by the asyncpg library itself, but below the level of the SQLAlchemy dialect.
References: #6199
Released: April 2, 2021
joinedload()
loader strategy would not successfully joinedload to a mapper that is mapper against a CTE
construct.
References: #6172
FunctionElement.render_derived()
feature where column names rendered out explicitly in the alias SQL would not have proper quoting applied for case sensitive names and other non-alphanumeric names.
References: #6183
Operators.in_()
method with a Select
object against a non-table-bound column would produce an AttributeError
, or more generally using a ScalarSelect
that has no datatype in a binary expression would produce invalid state.
References: #6181
Dialect
class called Dialect.supports_statement_cache
. This flag now needs to be present directly on a dialect class in order for SQLAlchemy’s query cache to take effect for that dialect. The rationale is based on discovered issues such as #6173 revealing that dialects which hardcode literal values from the compiled statement, often the numerical parameters used for LIMIT / OFFSET, will not be compatible with caching until these dialects are revised to use the parameters present in the statement only. For third party dialects where this flag is not applied, the SQL logging will show the message “dialect does not support caching”, indicating the dialect should seek to apply this flag once they have verified that no per-statement literal values are being rendered within the compilation phase.
See also
Caching for Third Party Dialects
References: #6184
Enum.omit_aliases
in Enum
type allow filtering aliases when using a pep435 Enum. Previous versions of SQLAlchemy kept aliases in all cases, creating database enum type with additional states, meaning that they were treated as different values in the db. For backward compatibility this flag defaults to False
in the 1.4 series, but will be switched to True
in a future version. A deprecation warning is raise if this flag is not specified and the passed enum contains aliases.
References: #6146
as_declarative()
needed to more fully add the DeclarativeMeta
class to the mypy interpreter’s state so that it does not result in a name not found error; additionally improves how global names are setup for the plugin including the Mapped
name.
References: #sqlalchemy/sqlalchemy2-stubs/#14
contextvars
installed.
References: #6166
ARRAY
when using psycopg2 would fail to use the correct type in the case that the datatype were also embedded within an instance of the Variant
adapter.
Additionally, repairs support for the correct CREATE TYPE to be emitted when using a Variant(ARRAY(some_schema_type))
.
This change is also backported to: 1.3.25
References: #6182
PGInspector
, when generated against an Engine
, would fail for .get_enums()
, .get_view_names()
, .get_foreign_table_names()
and .get_table_oid()
when used against a “future” style engine and not the connection directly.
References: #6170
offset=0
is used in a subquery.
References: #6163
Released: March 30, 2021
PropComparator.and_()
feature where loader strategies that emit secondary SELECT statements such as selectinload()
and lazyload()
would fail to accommodate for bound parameters in the user-defined criteria in terms of the current statement being executed, as opposed to the cached statement, causing stale bound values to be used.
This also adds a warning for the case where an object that uses lazyload()
in conjunction with PropComparator.and_()
is attempted to be serialized; the loader criteria cannot reliably be serialized and deserialized and eager loading should be used for this case.
References: #6139
Session.get()
from the ScopedSession
interface.
References: #6144
Transaction
so that an “already detached” warning is not emitted by the ending of the context manager itself, if the transaction were already manually rolled back inside the block. This applies to regular transactions, savepoint transactions, and legacy “marker” transactions. A warning is still emitted if the .rollback()
method is called explicitly more than once.
References: #6155
is_disconnect()
handler for the pg8000 dialect, which now accommodates for a new InterfaceError
emitted by pg8000 1.19.0. Pull request courtesy Hamdi Burak Usul.
References: #6099
importlib_metadata
library for loading setuptools entrypoints in order to accommodate for some deprecation changes.
Released: March 25, 2021
exec "statement"
and exec("statement")
do not behave the same way. The compatibility exec_()
function was used instead.
References: #6069
column_property()
would fail to correlate correctly to an enclosing subquery or to a CTE when Select.correlate_except()
were used in the property to control correlation, in cases where the subquery contained the same selectables as ones within the correlated subquery that were intended to not be correlated.
References: #6060
ClauseElement.params()
method can work correctly with a Select
object that includes joins across ORM relationship structures, which is a new feature in 1.4.
References: #6124
.metadata
attribute on a per class level would not be honored, breaking the use case of per-class-hierarchy MetaData
for abstract declarative classes and mixins.
See also
metadata
References: #6128
ResultProxy
name back to the sqlalchemy.engine
namespace. This name refers to the LegacyCursorResult
object.
References: #6119
Sequence
objects among the dropping of multiple tables, such that all Sequence
objects are dropped after all tables, even if the given Sequence
is related only to a Table
object and not directly to the overall MetaData
object. The use case supports the same Sequence
being associated with more than one Table
at a time.
This change is also backported to: 1.3.24
References: #6071
as_declarative()
function as well as the registry.as_declarative_base()
method.
Boolean
column type would produce an exception; additionally implemented support for Enum
, including detection of a string-based enum vs. use of Python enum.Enum
.
References: #6109
ARRAY.contains()
method.
This change is also backported to: 1.3.24
References: #6023
pysqlcipher
dialect to connect correctly which had regressed in 1.4, and added test + CI support to maintain the driver in working condition. The dialect now imports the sqlcipher3
module for Python 3 by default before falling back to pysqlcipher3
which is documented as now being unmaintained.
See also
Pysqlcipher
References: #5848
Released: March 19, 2021
declared_attr
object to work in the context of dataclass fields.
See also
Using Declarative Mixins with pre-existing dataclasses
References: #6100
Query.selectable
accessor, which is a synonym for Query.__clause_element__()
, got removed, it’s now restored.
References: #6088
Query.filter_by()
method would fail to locate the correct source entity if the Query.join()
method had been used targeting an entity without any kind of ON clause.
References: #6092
Function
would not work correctly if the object had been “annotated”, which is an internal memoization process used mostly by the ORM. In particular it could affect ORM lazy loads which make greater use of this feature in 1.4.
References: #6095
ConcreteBase
would fail to map at all when a mapped column name overlapped with the discriminator column name, producing an assertion error. The use case here did not function correctly in 1.3 as the polymorphic union would produce a query that ignored the discriminator column entirely, while emitting duplicate column warnings. As 1.4’s architecture cannot easily reproduce this essentially broken behavior of 1.3 at the select()
level right now, the use case now raises an informative error message instructing the user to use the .ConcreteBase._concrete_discriminator_name
attribute to resolve the conflict. To assist with this configuration, .ConcreteBase._concrete_discriminator_name
may be placed on the base class only where it will be automatically used by subclasses; previously this was not the case.
References: #6090
sqlalchemy.engine.reflection
. This ensures that the base Inspector
class is properly registered so that inspect()
works for third party dialects that don’t otherwise import this package.
func
that includes dotted packagenames would fail to be cacheable by the SQL caching system due to a Python list of names that needed to be a tuple.
References: #6101
case()
construct, where the “dictionary” form of argument specification failed to work correctly if it were passed positionally, rather than as a “whens” keyword argument.
References: #6097
Column
if the type were given with a module prefix like sa.Integer()
.
References: #sqlalchemy/sqlalchemy2-stubs/2
Released: March 17, 2021
select()
using ORM entities would eagerly configure the mappers, in an effort to maintain compatibility with the Query
object which necessarily does this to support many backref-related legacy cases. However, core select()
constructs are also used in mapper configurations and such, and to that degree this eager configuration is more of an inconvenience, so eager configure has been disabled for the select()
and other Core constructs in the absence of ORM loading types of functions such as Load
.
The change maintains the behavior of Query
so that backwards compatibility is maintained. However, when using a select()
in conjunction with ORM entities, a “backref” that isn’t explicitly placed on one of the classes until mapper configure time won’t be available unless configure_mappers()
or the newer configure()
has been called elsewhere. Prefer using relationship.back_populates
for more explicit relationship configuration which does not have the eager configure requirement.
References: #6066
Query.exists()
method would fail to create an expression if the entity list of the Query
were an arbitrary SQL column expression.
References: #6076
Query.count()
in conjunction with a loader option such as joinedload()
would fail to ignore the loader option. This is a behavior that has always been very specific to the Query.count()
method; an error is normally raised if a given Query
has options that don’t apply to what it is returning.
References: #6052
Session.identity_key()
, including that the method and related methods were not covered by any unit test as well as that the method contained a typo preventing it from functioning correctly.
References: #6067
DeclarativeMeta
. While the attribute remains something that can be set explicitly on a declarative base to be consumed by the metaclass, once located it is placed under a private class variable so it does not conflict with future subclasses that use the same name for other purposes.
References: #6054
namedtuple()
has the behavior such that the names count
and index
will be served as tuple values if the named tuple includes those names; if they are absent, then their behavior as methods of collections.abc.Sequence
is maintained. Therefore the Row
and LegacyRow
classes have been fixed so that they work in this same way, maintaining the expected behavior for database rows that have columns named “index” or “count”.
References: #6074
use_setinputsizes
is passed to the dialect, e.g. via create_engine()
, at which point its behavior can be customized using the DialectEvents.do_setinputsizes()
hook.
See also
Setinputsizes Support
References: #6058
items
and values
to ColumnCollection
class. The regression was introduced while adding support for duplicate columns in from clauses and selectable in ticket #4753.
References: #6068
Released: March 15, 2021
NoReferenceError
within the join process, which nonetheless could be bypassed to allow the join to complete. The logic which tested the exception for significance within the process would make assumptions about the construct which would fail.
This change is also backported to: 1.3.24
References: #5952
MutableComposite
construct could be placed into an invalid state when the parent object was already loaded, and then covered by a subsequent query, due to the composite properties’ refresh handler replacing the object with a new one not handled by the mutable extension.
This change is also backported to: 1.3.24
References: #6001
relationship.query_class
parameter stopped being functional for “dynamic” relationships. The AppenderQuery
remains dependent on the legacy Query
class; users are encouraged to migrate from the use of “dynamic” relationships to using with_parent()
instead.
References: #5981
Query.join()
would produce no effect if the query itself as well as the join target were against a Table
object, rather than a mapped class. This was part of a more systemic issue where the legacy ORM query compiler would not be correctly used from a Query
if the statement produced had not ORM entities present within it.
References: #6003
AsyncSession.delete()
is now an awaitable; this method cascades along relationships which must be loaded in a similar manner as the AsyncSession.merge()
method.
References: #5998
DefaultGenerator
objects such as sequences, which included the case where they were “pre-executed” in order to generate primary key values when implicit_returning was disabled.
This change is also backported to: 1.3.24
References: #5929
Connection
when it were closed, and also leading to a double-rollback scenario that was somewhat wasteful. The newer architecture of the engine has been updated so that the connection pool “reset-on-return” logic will be skipped when the Connection
explicitly closes out the transaction before returning the pool to the connection.
References: #6004
CTE
construct so that a string is returned representing the inner SELECT statement if the CTE
is stringified directly, outside of the context of an enclosing SELECT; This is the same behavior of FromClause.alias()
and Select.subquery()
. Previously, a blank string would be returned as the CTE is normally placed above a SELECT after that SELECT has been generated, which is generally misleading when debugging.
Operators.op()
and custom_op
constructs, for custom operators that use percent signs. The percent sign will now be automatically doubled based on the paramstyle as necessary.
References: #6016
distinct()
used in the form of being directly SELECTed would fail to be locatable in the result set by column identity, which is how the ORM locates columns. While standalone distinct()
is not oriented towards being directly SELECTed (use select.distinct()
for a regular SELECT DISTINCT..
) , it was usable to a limited extent in this way previously (but wouldn’t work in subqueries, for example). The column targeting for unary expressions such as “DISTINCT ” has been improved so that this case works again, and an additional improvement has been made so that usage of this form in a subquery at least generates valid SQL which was not the case previously.
The change additionally enhances the ability to target elements in row._mapping
based on SQL expression objects in ORM-enabled SELECT statements, including whether the statement was invoked by connection.execute()
or session.execute()
.
References: #6008
Boolean
or Enum
would fail to render the naming convention correctly after the first compilation, due to an unintended change of state within the name given to the constraint. This issue was first introduced in 0.9 in the fix for issue #3067, and the fix revises the approach taken at that time which appears to have been more involved than what was needed.
This change is also backported to: 1.3.24
References: #6007
PrimaryKeyConstraint
object that’s automatically associated with a Table
will update its name as new primary key Column
objects are added to the table and then to the constraint. Internal failure modes related to this constraint construction process including no columns present, no name present or blank name present are now accommodated.
This change is also backported to: 1.3.24
References: #5919
.copy()
methods and renamed to _copy()
. These are not standard Python “copy()” methods as they typically rely upon being instantiated within particular contexts which are passed to the method as optional keyword arguments. The Table.tometadata()
method is the public API that provides copying for Table
objects.
References: #5953
asyncio.Lock()
within SQLAlchemy’s emulated DBAPI cursor, local to the connection, for the asyncpg and aiomysql dialects for the scope of the cursor.execute()
and cursor.executemany()
methods. The rationale is to prevent failures and corruption for the case where the connection is used in multiple awaitables at once.
While this use case can also occur with threaded code and non-asyncio dialects, we anticipate this kind of use will be more common under asyncio, as the asyncio API is encouraging of such use. It’s definitely better to use a distinct connection per concurrent awaitable however as concurrency will not be achieved otherwise.
For the asyncpg dialect, this is so that the space between the call to prepare()
and fetch()
is prevented from allowing concurrent executions on the connection from causing interface error exceptions, as well as preventing race conditions when starting a new transaction. Other PostgreSQL DBAPIs are threadsafe at the connection level so this intends to provide a similar behavior, outside the realm of server side cursors.
For the aiomysql dialect, the mutex will provide safety such that the statement execution and the result set fetch, which are two distinct steps at the connection level, won’t get corrupted by concurrent executions on the same connection.
References: #5967
aggregate_order_by
would return ARRAY(NullType) under certain conditions, interfering with the ability of the result object to return data correctly.
This change is also backported to: 1.3.24
References: #5989
AutomapBase.prepare.reflection_options
to allow passing of MetaData.reflect()
options like only
or dialect-specific reflection options like oracle_resolve_synonyms
.
References: #5942
sqlalchemy.ext.mutable
extension now tracks the “parents” collection using the InstanceState
associated with objects, rather than the object itself. The latter approach required that the object be hashable so that it can be inside of a WeakKeyDictionary
, which goes against the behavioral contract of the ORM overall which is that ORM mapped objects do not need to provide any particular kind of __hash__()
method and that unhashable objects are supported.
References: #6020
Released: February 15, 2021
Select.from_statement()
in an ORM context.
See also
Using INSERT, UPDATE and ON CONFLICT (i.e. upsert) to return ORM Objects
set_
keyword of OnConflictDoUpdate
to accept a ColumnCollection
, such as the .c.
collection from a Selectable
, or the .excluded
contextual object.
References: #5939
Function.in_()
method was not covered by tests and failed to function properly in all cases.
References: #5934
select()
function was not working, outside of plain lists. The forwards/backwards compatibility logic here now checks for a wider range of incoming “iterable” types including that a .c
collection from a selectable can be passed directly. Pull request compliments of Oliver Rice.
References: #5935
Released: February 3, 2021
ORMExecuteState.bind_mapper
and ORMExecuteState.all_mappers
accessors to ORMExecuteState
event object, so that handlers can respond to the target mapper and/or mapped class or classes involved in an ORM statement execution.
AsyncSession.scalar()
, AsyncSession.get()
as well as support for sessionmaker.begin()
to work as an async context manager with AsyncSession
. Also added AsyncSession.in_transaction()
accessor.
References: #5796, #5797, #5802
configure_mappers()
function, is now organized to be on a per-registry basis. This allows for example the mappers within a certain declarative base to be configured, but not those of another base that is also present in memory. The goal is to provide a means of reducing application startup time by only running the “configure” process for sets of mappers that are needed. This also adds the registry.configure()
method that will run configure for the mappers local in a particular registry only.
References: #5897
relationship.secondary
. This is an extremely common error which warrants a clear message.
Additionally, added a new rule to the class registry resolution such that with regards to the relationship.secondary
parameter, if a mapped class and its table are of the identical string name, the Table
will be favored when resolving this parameter. In all other cases, the class continues to be favored if a class and table share the identical name.
This change is also backported to: 1.3.21
References: #5774
restore_load_context
option of ORM events such as InstanceEvents.load()
such that the flag would not be carried along to subclasses which were mapped after the event handler were first established.
This change is also backported to: 1.3.21
References: #5737
Session
similar to that of the Connection
where the new “autobegin” logic could be tripped into a re-entrant (recursive) state if SQL were executed within the SessionEvents.after_transaction_create()
event hook.
References: #5845
Bundle.single_entity
flag would take effect for a Bundle
even though it were not set. Additionally, this flag is legacy as it only makes sense for the Query
object and not 2.0 style execution. a deprecation warning is emitted when used with new-style execution.
References: #5702
aliased
construct against a plain selectable and including a name would raise an assertionerror.
References: #5750
with_loader_criteria()
feature as well as the SessionEvents.do_orm_execute()
event handler that is often used in conjunction [ticket:5760]:
with_loader_criteria()
function would fail if the given entity or base included non-mapped mixins in its descending class hierarchy [ticket:5766]
with_loader_criteria()
feature is now unconditionally disabled for the case of ORM “refresh” operations, including loads of deferred or expired column attributes as well as for explicit operations like Session.refresh()
. These loads are necessarily based on primary key identity where additional WHERE criteria is never appropriate. [ticket:5762]
ORMExecuteState.is_column_load
to indicate that a SessionEvents.do_orm_execute()
handler that a particular operation is a primary-key-directed column attribute load, where additional criteria should not be added. The with_loader_criteria()
function as above ignores these in any case now. [ticket:5761]
ORMExecuteState.is_relationship_load
attribute would not be set correctly for many lazy loads as well as all selectinloads. The flag is essential in order to test if options should be added to statements or if they would already have been propagated via relationship loads. [ticket:5764]
Query.having()
in conjunction with queries with internally adapted SQL elements (common in inheritance scenarios) would fail due to an incorrect function call. Pull request courtesy esoh.
References: #5781
sqlalchemy.ext.compiles
extension according to the documentation that’s been up for many years would no longer function if only Executable, ClauseElement
were used as the base classes, additional classes were needed if wanting to use Session.execute()
. This has been resolved so that those extra classes aren’t needed.
Mapper.primary_key
to establish certain columns as “primary”.
References: #5867
Insert.on_conflict_do_update()
can now stringify in-place without the need to specify an explicit dialect object. The constructs, when called upon for str()
, print()
, etc. now have internal direction to call upon their appropriate dialect rather than the “default”dialect which doesn’t know how to stringify these. The approach is also adapted to generic schema-level create/drop such as AddConstraint
, which will adapt its stringify dialect to one indicated by the element within it, such as the ExcludeConstraint
object.
Connection.execution_options.logging_token
. This option will add an additional per-message token to log messages generated by the Connection
as it executes statements. This token is not part of the logger name itself (that part can be affected using the existing create_engine.logging_name
parameter), so is appropriate for ad-hoc connection use without the side effect of creating many new loggers. The option can be set at the level of Connection
or Engine
.
See also
Setting Per-Connection / Sub-Engine Tokens
References: #5911
Engine
where emitting SQL during the EngineEvents.begin()
event hook would cause a re-entrant (recursive) condition due to autobegin, affecting among other things the recipe documented for SQLite to allow for savepoints and serializable isolation support.
References: #5845
TypeDecorator
that includes an override the TypeDecorator.get_dbapi_type()
method.
engine_from_config()
function, so that the values “true” and “false” may be configured as “boolean” values when using a key such as sqlalchemy.future = true
or sqlalchemy.future = false
.
FunctionElement.table_valued()
modifier that creates a table-like selectable object from a SQL function
TableValuedAlias
construct that renders a SQL function as a named table
json_to_recordset
function, using the TableValuedAlias.render_derived()
method.
FunctionElement.table_valued.with_ordinality
parameter
FunctionElement.column_valued()
method
FunctionElement.scalar_table_valued()
method.
See also Table-Valued Functions - in the SQLAlchemy Unified Tutorial References: #3566
Insert.returning()
, may now be chained to add new columns to the RETURNING clause.
References: #5695
Select.outerjoin_from()
method to complement Select.join_from()
.
Compiler
to render NULL for a bound parameter that has None
as the value, either explicitly passed or omitted. The previous error message “bind parameter without a renderable value” is removed, and a missing or None
value will now render NULL in all cases. Previously, rendering of NULL was starting to happen for DML statements due to internal refactorings, but was not explicitly part of test coverage, which it now is.
While no error is raised, when the context is within that of a column comparison, and the operator is not “IS”/”IS NOT”, a warning is emitted that this is not generally useful from a SQL perspective.
References: #5888
Select.join()
method where chaining from the current JOIN wasn’t looking at the right state, causing an expression like “FROM a JOIN b , b JOIN c ” rather than “FROM a JOIN b JOIN c ”.
References: #5858
Session.execute()
.
References: #5754
with_loader_criteria()
feature where it is most prominently used [ticket:5760]:
in_()
expression against a list of SQL elements, such as literal()
objects, would fail to be accommodated correctly. [ticket:5768]
Insert
constructs) if they are called a second time, which would implicitly cancel out the previous setting. The methods altered include: on_conflict_do_update
, on_conflict_do_nothing
(SQLite), on_conflict_do_update
, on_conflict_do_nothing
(PostgreSQL), on_duplicate_key_update
(MySQL)
References: #5169
Values
construct where passing tuples of objects would fall back to per-value type detection rather than making use of the Column
objects passed directly to Values
that tells SQLAlchemy what the expected type is. This would lead to issues for objects such as enumerations and numpy strings that are not actually necessary since the expected type is given.
References: #5785
RemovedIn20Warning
would erroneously emit when the .bind
attribute were accessed internally on objects, particularly when stringifying a SQL construct.
References: #5717
cycle=False
and order=False
as NO CYCLE
and NO ORDER
in Sequence
and Identity
objects.
References: #5722
Query.with_labels()
and GenerativeSelect.apply_labels()
with explicit getters and setters GenerativeSelect.get_label_style()
and GenerativeSelect.set_label_style()
to accommodate the three supported label styles: LABEL_STYLE_DISAMBIGUATE_ONLY
, LABEL_STYLE_TABLENAME_PLUS_COL
, and LABEL_STYLE_NONE
.
In addition, for Core and “future style” ORM queries, LABEL_STYLE_DISAMBIGUATE_ONLY
is now the default label style. This style differs from the existing “no labels” style in that labeling is applied in the case of column name conflicts; with LABEL_STYLE_NONE
, a duplicate column name is not accessible via name in any case.
For cases where labeling is significant, namely that the .c
collection of a subquery is able to refer to all columns unambiguously, the behavior of LABEL_STYLE_DISAMBIGUATE_ONLY
is now sufficient for all SQLAlchemy features across Core and ORM which involve this behavior. Result set