Mike Bayer [Mon, 20 Apr 2026 13:30:35 +0000 (09:30 -0400)]
narrow scope of _correct_for_mysql_bugs_88718_96365
Narrowed the scope of the internal workaround for MySQL bugs `#88718
<https://bugs.mysql.com/bug.php?id=88718>`_ and `#96365
<https://bugs.mysql.com/bug.php?id=96365>`_ so that it is only applied
where needed: MySQL 8.0.1 through 8.0.13 (where bug 88718 is present), and
on systems with ``lower_case_table_names=2`` (where bug 96365 applies,
typically macOS). Previously the workaround was applied unconditionally
for all MySQL 8.0+ versions, which caused a ``KeyError`` during foreign key
reflection when the database user lacked SELECT privileges on referred
tables.
Mike Bayer [Fri, 17 Apr 2026 20:13:53 +0000 (16:13 -0400)]
handle asyncpg InternalClientError
Fixed issue where the asyncpg driver could throw an insufficiently-handled
exception ``InternalClientError`` under some circumstances, leading to
connections not being properly marked as invalidated.
Improve handling of two phase transaction identifiers for PostgreSQL
when the identifier is provided by the user.
As part of this change the psycopg dialect was updated to use the DBAPI
two phase transaction API instead of executing the SQL directly.
Mike Bayer [Sun, 29 Mar 2026 17:46:39 +0000 (13:46 -0400)]
accommodate subclass mapper in post-loader entity_isa check
Fixed issue where using chained loader options such as
:func:`_orm.selectinload` after :func:`_orm.joinedload` with
:meth:`_orm.PropComparator.of_type` for a polymorphic relationship would
not properly apply the chained loader option. The loader option is now
correctly applied when using a call such as
``joinedload(A.b.of_type(poly)).selectinload(poly.SubClass.c)`` to eagerly
load related objects.
Mike Bayer [Fri, 27 Mar 2026 18:24:10 +0000 (14:24 -0400)]
apply _path_with_polymorphic in prepend as well
Fixed issue where using :meth:`_orm.Load.options` to apply a chained loader
option such as :func:`_orm.joinedload` or :func:`_orm.selectinload` with
:meth:`_orm.PropComparator.of_type` for a polymorphic relationship would
not generate the necessary clauses for the polymorphic subclasses. The
polymorphic loading strategy is now correctly propagated when using a call
such as ``joinedload(A.b).options(joinedload(B.c.of_type(poly)))`` to match
the behavior of direct chaining e.g.
``joinedload(A.b).joinedload(B.c.of_type(poly))``.
joshuaswanson [Fri, 27 Mar 2026 16:41:11 +0000 (12:41 -0400)]
fix: Session.get() with with_for_update=False skips identity map
Fixes #13176.
`Session.get()` checks `with_for_update is None` to decide whether to look up the identity map. Passing `with_for_update=False` fails this check and always hits the database, even though `ForUpdateArg._from_argument` already treats `False` and `None` identically (both return `None`). Changed to `with_for_update in (None, False)` to match.
Mike Bayer [Mon, 23 Mar 2026 18:13:02 +0000 (14:13 -0400)]
detect and accommodate reverse condition for loader strategy
Fixed issue where chained :func:`_orm.joinedload` options would not be
applied correctly when the final relationship in the chain is declared on a
base mapper and accessed through a subclass mapper in a
:func:`_orm.with_polymorphic` query. The path registry now correctly
computes the natural path when a property declared on a base class is
accessed through a path containing a subclass mapper, ensuring the loader
option can be located during query compilation.
Carlos Serrano [Wed, 18 Mar 2026 15:37:08 +0000 (11:37 -0400)]
mssql: fall back to base type for alias types during reflection
Fixed regression from version 2.0.42 caused by :ticket:`12654` where the
updated column reflection query would receive SQL Server "type alias" names
for special types such as ``sysname``, whereas previously the base name
would be received (e.g. ``nvarchar`` for ``sysname``), leading to warnings
that such types could not be reflected and resulting in :class:`.NullType`,
rather than the expected :class:`.NVARCHAR` for a type like ``sysname``.
The column reflection query now joins ``sys.types`` a second time to look
up the base type when the user type name is not present in
:attr:`.MSDialect.ischema_names`, and both names are checked in
:attr:`.MSDialect.ischema_names` for a match. Pull request courtesy Carlos
Serrano.
Mike Bayer [Thu, 19 Mar 2026 00:21:20 +0000 (20:21 -0400)]
remove cx_oracle from testing
cx_oracle is no longer able to build from its .tar.gz form
reliably because it does not include setuptools in its build
dependencies. It still can be built if pip is given
--no-build-isolation, or if a wheel file is installed rather than
the .tar.gz, but given how quickly cx_oracle has been pushed
aside by oracledb it's not really that important to be testing
it anymore.
medovi40k [Sat, 28 Feb 2026 23:10:17 +0000 (18:10 -0500)]
Add typing overloads to Query.__getitem__ and AppenderQuery.__getitem__
Fixes #13128
### Description
`Query.__getitem__` and `AppenderQuery.__getitem__` previously returned Union[_T, List[_T]] for all inputs, making the return type inaccurate.
Added `@overload` signatures so that integer index returns _T and slice returns List[_T].
This pull request is:
- [x] A documentation / typographical / small typing error fix
- Good to go, no issue or tests are needed
Hello! This is my first PR here, so please let me know what I may have missed in terms of having a valuable contribution. I was looking through issues to grab an easy first one, and found this. Looks like someone else was going to have a go at it, but never did.
I simply added a small change to the FK regex in for Postgres that allows anything not quotes alongside escaped double quotes. Test is included for the scenario mentioned in the issue. Alongside that, I didn't see a test for general quoted strings, so I added another one that includes spaces and dashes, in my experience common things to be used inside quoted identifiers.
A manual test as well:
DB setup:
```
austin_test_bug=# CREATE TABLE """test_parent_table-quoted""" (id SERIAL PRIMARY KEY, val INTEGER);
CREATE TABLE
austin_test_bug=# CREATE TABLE test_child_table_ref_quoted (id SERIAL, parent INTEGER, CONSTRAINT fk_parent FOREIGN KEY (parent) REFERENCES """test_parent_table-quoted"""(id));
CREATE TABLE
austin_test_bug=# \d+
List of relations
Schema | Name | Type | Owner | Persistence | Access method | Size | Description
--------+------------------------------------+----------+----------+-------------+---------------+------------+-------------
public | "test_parent_table-quoted" | table | postgres | permanent | heap | 0 bytes |
public | "test_parent_table-quoted"_id_seq | sequence | postgres | permanent | | 8192 bytes |
public | test_child_table_ref_quoted | table | postgres | permanent | heap | 0 bytes |
public | test_child_table_ref_quoted_id_seq | sequence | postgres | permanent | | 8192 bytes |
(4 rows)
### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)
-->
This pull request is:
- [ ] A documentation / typographical / small typing error fix
- Good to go, no issue or tests are needed
- [x] A short code fix
- please include the issue number, and create an issue if none exists, which
must include a complete example of the issue. one line code fixes without an
issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests. one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.
Federico Caselli [Wed, 18 Mar 2026 19:45:39 +0000 (20:45 +0100)]
Remove version warning in SQL Server
Remove warning for SQL Server dialect when a new version is detected.
The warning was originally added more than 15 years ago due to an unexpected
value returned when using an old version of FreeTDS.
The assumption is that since then the issue has been resolved, so make the
SQL Server dialect behave like the other ones that don't have an upper bound
check on the version number.
Federico Caselli [Thu, 12 Mar 2026 22:34:27 +0000 (23:34 +0100)]
ensure function classes are not shadowed
Ensure the _FunctionGenerator method do not shadow the function class
of the same name
Fixed a typing issue where the typed members of :data:`.func` would return
the appropriate class of the same name, however this creates an issue for
typecheckers such as Zuban and pyrefly that assume :pep:`749` style
typechecking even if the file states that it's a :pep:`563` file; they see
the returned name as indicating the method object and not the class object.
These typecheckers are actually following along with an upcoming test
harness that insists on :pep:`749` style name resolution for this case
unconditionally. Since :pep:`749` is the way of the future regardless,
differently-named type aliases have been added for these return types.
Martin Baláž [Mon, 9 Mar 2026 17:19:04 +0000 (13:19 -0400)]
Update _NamingSchemaCallable to support Index
<!-- Provide a general summary of your proposed changes in the Title field above -->
### Description
<!-- Describe your changes in detail -->
According to [the documentation](https://docs.sqlalchemy.org/en/21/core/metadata.html#sqlalchemy.schema.MetaData.params.naming_convention), the values associated with user-defined “token” keys in `naming_convention` should be callables of the form `fn(constraint, table)`, which accepts the constraint/index object and Table. However, the type alias `_NamingSchemaCallable` accepts only constraint in the first argument. I propose to update `_NamingSchemaCallable` to accept also an index.
### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)
-->
This pull request is:
- [x] A documentation / typographical / small typing error fix
- Good to go, no issue or tests are needed
- [ ] A short code fix
- please include the issue number, and create an issue if none exists, which
must include a complete example of the issue. one line code fixes without an
issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests. one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.
Georg Sieber [Wed, 4 Mar 2026 23:24:44 +0000 (18:24 -0500)]
Add fast_executemany property to asyncadapt aioodbc cursor
Enhanced the ``aioodbc`` dialect to expose the ``fast_executemany``
attribute of the pyodbc cursor. This allows the ``fast_executemany``
parameter to work with the ``mssql+aioodbc`` dialect. Pull request
courtesy Georg Sieber.
Mike Bayer [Sun, 1 Mar 2026 18:05:21 +0000 (13:05 -0500)]
make local mutable copies for cargs / cparams in do_connect
Fixed a critical issue in :class:`.Engine` where connections created in
conjunction with the :meth:`.ConnectionEvents.do_connect` event listeners
would receive shared, mutable collections for the connection arguments,
leading to a variety of potential issues including unlimited growth of the
argument list as well as elements within the parameter dictionary being
shared among concurrent connection calls. In particular this could impact
do_connect routines making use of complex mutable authentication
structures.
Kadir Can Ozden [Sat, 21 Feb 2026 11:35:38 +0000 (06:35 -0500)]
Fix WeakSequence.__getitem__ catching KeyError instead of IndexError
### Description
`WeakSequence.__getitem__` catches `KeyError` but the internal `_storage` is a `list`, which raises `IndexError` for out-of-range access. This means the `except KeyError` handler never executes, and the custom error message is never shown.
### Current behavior
```python
def __getitem__(self, index):
try:
obj = self._storage[index] # _storage is a list
except KeyError: # lists don't raise KeyError
raise IndexError("Index %s out of range" % index)
else:
return obj()
```
On an out-of-range index, the raw `IndexError` from list access propagates directly (e.g., `list index out of range`) instead of the intended custom message.
### Fix
Changed `except KeyError` to `except IndexError` so the handler actually catches the exception raised by list indexing.
Tiansu Yu [Fri, 20 Feb 2026 14:20:40 +0000 (09:20 -0500)]
Mysql ddl compiler fall back to default index args
Fixed issue where DDL compilation options were registered to the hard-coded
dialect name ``mysql``. This made it awkward for MySQL-derived dialects
like MariaDB, StarRocks, etc. to work with such options when different sets
of options exist for different platforms. Options are now registered under
the actual dialect name, and a fallback was added to help avoid errors when
an option does not exist for that dialect. Pull request courtesy Tiansu Yu.
Mike Bayer [Tue, 17 Feb 2026 20:58:22 +0000 (15:58 -0500)]
downgrade batches for bindparam() in SET
Fixed issue where :meth:`_postgresql.Insert.on_conflict_do_update`
using parametrized bound parameters in the ``set_`` clause would fail
when used with executemany batching. For dialects that use the
``use_insertmanyvalues_wo_returning`` optimization (psycopg2),
insertmanyvalues is now disabled when there is an ON CONFLICT clause.
For cases with RETURNING, row-at-a-time mode is used when the SET
clause contains parametrized bindparams (bindparams that receive
values from the parameters dict), ensuring each row's parameters are
correctly applied. ON CONFLICT statements using expressions like
``excluded.<column>`` continue to batch normally.
Fixed issue where :meth:`_sqlite.Insert.on_conflict_do_update`
using parametrized bound parameters in the ``set_`` clause would fail
when used with executemany batching. Row-at-a-time mode is now used
for ON CONFLICT statements with RETURNING that contain parametrized
bindparams, ensuring each row's parameters are correctly applied. ON
CONFLICT statements using expressions like ``excluded.<column>``
continue to batch normally.
The connection object returned by :meth:`_engine.Engine.raw_connection`
now supports the context manager protocol, automatically returning the
connection to the pool when exiting the context.
Fixed issue where :meth:`_postgresql.Insert.on_conflict_do_update`
as well as :meth:`_sqlite.Insert.on_conflict_do_update`
parameters were not respecting compilation options such as
``literal_binds=True``.
Mike Bayer [Wed, 4 Feb 2026 02:07:59 +0000 (21:07 -0500)]
allow batch with upsert if embed_values_counter is True
Fixed issue in the :ref:`engine_insertmanyvalues` feature where using
PostgreSQL's ``ON CONFLICT`` clause with
:paramref:`_dml.Insert.returning.sort_by_parameter_order` enabled would
generate invalid SQL when the insert used an implicit sentinel (server-side
autoincrement primary key). The generated SQL would incorrectly declare a
sentinel counter column in the ``imp_sen`` table alias without providing
corresponding values in the ``VALUES`` clause, leading to a
``ProgrammingError`` indicating column count mismatch. The fix allows batch
execution mode when ``embed_values_counter`` is active, as the embedded
counter provides the ordering capability needed even with upsert behaviors,
rather than unnecessarily downgrading to row-at-a-time execution.
Mike Bayer [Tue, 3 Feb 2026 21:11:13 +0000 (16:11 -0500)]
parse ON UPDATE / ON DELETE in any order
Fixed an issue in the PostgreSQL dialect where foreign key constraint
reflection would incorrectly swap or fail to capture ``onupdate`` and
``ondelete`` values when these clauses appeared in a different order than
expected in the constraint definition. This issue primarily affected
PostgreSQL-compatible databases such as CockroachDB, which may return ``ON
DELETE`` before ``ON UPDATE`` in the constraint definition string. The
reflection logic now correctly parses both clauses regardless of their
ordering.
Mike Bayer [Tue, 3 Feb 2026 13:53:53 +0000 (08:53 -0500)]
forwards-port cpython issue 141560 for getfullargspec
Fixed issue when using ORM mappings with Python 3.14's :pep:`649` feature
that no longer requires "future annotations", where the ORM's introspection
of the ``__init__`` method of mapped classes would fail if non-present
identifiers in annotations were present. The vendored ``getfullargspec()``
method has been amended to use ``Format.FORWARDREF`` under Python 3.14 to
prevent resolution of names that aren't present.
Mike Bayer [Fri, 30 Jan 2026 15:32:52 +0000 (10:32 -0500)]
allow 2 uncleared connections at most
tests show that if the code is really broken, we have
5 or more connections lingering here, so for less than two
(it's usually one) just clean it out and consider it as GC noise.
To test this better we also open up the windows/mac archs that
were disabled for greenlet
Gord Thompson [Mon, 19 Jan 2026 12:34:28 +0000 (05:34 -0700)]
make qtoken for PostgreSQL _fk_regex_pattern less restrictive
Improved the foreign key reflection regular expression pattern used by the
PostgreSQL dialect to be more permissive in matching identifier characters,
allowing it to correctly handle unicode characters in table and column
names. This change improves compatibility with PostgreSQL variants such as
CockroachDB that may use different quoting patterns in combination with
unicode characters in their identifiers. Pull request courtesy Gord
Thompson.
rusher [Wed, 14 Jan 2026 14:03:00 +0000 (09:03 -0500)]
correct mariadb sequence behavior when cycle=False
Fixed the SQL compilation for the mariadb sequence "NOCYCLE" keyword that
is to be emitted when the :paramref:`.Sequence.cycle` parameter is set to
False on a :class:`.Sequence`. Pull request courtesy Diego Dupin.
Mike Bayer [Tue, 13 Jan 2026 14:19:14 +0000 (09:19 -0500)]
typing updates to accept with_polymorphic(), aliases
Fixed typing issues where ORM mapped classes and aliased entities could not
be used as keys in result row mappings or as join targets in select
statements. Patterns such as ``row._mapping[User]``,
``row._mapping[aliased(User)]``, ``row._mapping[with_polymorphic(...)]``
(rejected by both mypy and Pylance), and ``.join(aliased(User))``
(rejected by Pylance) are documented and fully supported at runtime but
were previously rejected by type checkers. The type definitions for
:class:`._KeyType` and :class:`._FromClauseArgument` have been updated to
accept these ORM entity types.
Mike Bayer [Wed, 7 Jan 2026 01:03:10 +0000 (20:03 -0500)]
apply Grouping on left side of JSONB subscript in compiler
Fixed regression in PostgreSQL dialect where JSONB subscription syntax
would generate incorrect SQL for :func:`.cast` expressions returning JSONB,
causing syntax errors. The dialect now properly wraps cast expressions in
parentheses when using the ``[]`` subscription syntax, generating
``(CAST(...))[index]`` instead of ``CAST(...)[index]`` to comply with
PostgreSQL syntax requirements. This extends the fix from :ticket:`12778`
which addressed the same issue for function calls.
This reverts how we did the fix for #12778 in Function.self_group()
and instead moves to a direct Grouping() applied in the PG compiler
based on isinstance of the left side.
in retrospect, when we first did #10927, we **definitely** made
the completely wrong choice in how to do this, the original idea
to detect when we were in an UPDATE and use [] only then was
by **far** what we should have done, given the fact that PG indexes
are based on exact syntax matches. but since we've made everyone
switch to [] format for their indexes now we can't keep going
back and forth. even though PG would like [] to be the defacto
syntax it simply is not. We should potentially pursue a dialect/
create_engine option to switch the use of [] back to -> for
all cases except UPDATE.
Federico Caselli [Wed, 24 Dec 2025 13:02:17 +0000 (14:02 +0100)]
Support aiosqlite 0.22.0+
Fixed issue in the aiosqlite driver where SQLAlchemy's setting of
aiosqlite's worker thread to "daemon" stopped working because the aiosqlite
architecture moved the location of the worker thread in version 0.22.0.
This "daemon" flag is necessary so that a program is able to exit if the
SQLite connection itself was not explicitly closed, which is particularly
likely with SQLAlchemy as it maintains SQLite connections in a connection
pool. While it's perfectly fine to call :meth:`.AsyncEngine.dispose`
before program exit, this is not historically or technically necessary for
any driver of any known backend, since a primary feature of relational
databases is durability. The change also implements support for
"terminate" with aiosqlite when using version version 0.22.1 or greater,
which implements a sync ``.stop()`` method.
Mike Bayer [Wed, 31 Dec 2025 20:48:44 +0000 (15:48 -0500)]
Fixed JSONB path_match and path_exists operators to use correct type coercion
Fixed issue where PostgreSQL JSONB operators
:meth:`_postgresql.JSONB.Comparator.path_match` and
:meth:`_postgresql.JSONB.Comparator.path_exists` were applying incorrect
``VARCHAR`` casts to the right-hand side operand when used with newer
PostgreSQL drivers such as psycopg. The operators now indicate the
right-hand type as ``JSONPATH``, which currently results in no casting
taking place, but is also compatible with explicit casts if the
implementation were require it at a later point.
Support for `IF EXISTS` in SQL Server 2016 (13.x) and later versions
Added support for the ``IF EXISTS`` clause when dropping indexes on SQL
Server 2016 (13.x) and later versions. The :paramref:`.DropIndex.if_exists`
parameter is now honored by the SQL Server dialect, allowing conditional
index drops that will not raise an error if the index does not exist.
Pull request courtesy Edgar Ramírez Mondragón.
<!-- Provide a general summary of your proposed changes in the Title field above -->
<!-- Describe your changes in detail -->
`Query[_T].get(...)` should return `Optional[_T]` instead of `Optional[Any]`. This is typed correctly when migrating to `Session.get(_T, ...)`. By typing the legacy `Query.get(...)` call first, it should make migrations easier on developers, as it splits up the type checking improvements (and subsequent errors which may be discovered) from the `Query.get()`
to `Session.get()` migration.
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)
-->
This pull request is:
- [x] A documentation / typographical / small typing error fix
- Good to go, no issue or tests are needed
- [ ] A short code fix
- please include the issue number, and create an issue if none exists, which
must include a complete example of the issue. one line code fixes without an
issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests. one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.
G Allajmi [Tue, 9 Dec 2025 19:13:52 +0000 (14:13 -0500)]
Factor out constraints into separate methods
Fixed issue where PostgreSQL dialect options such as ``postgresql_include``
on :class:`.PrimaryKeyConstraint` and :class:`.UniqueConstraint` were
rendered in the wrong position when combined with constraint deferrability
options like ``deferrable=True``. Pull request courtesy G Allajmi.
G Allajmi [Mon, 8 Dec 2025 13:03:49 +0000 (08:03 -0500)]
Fix adding property to mapper before mapping is complete
Fixed issue where calling :meth:`.Mapper.add_property` within mapper event
hooks such as :meth:`.MapperEvents.instrument_class`,
:meth:`.MapperEvents.after_mapper_constructed`, or
:meth:`.MapperEvents.before_mapper_configured` would raise an
``AttributeError`` because the mapper's internal property collections were
not yet initialized. The :meth:`.Mapper.add_property` method now handles
early-stage property additions correctly, allowing properties including
column properties, deferred columns, and relationships to be added during
mapper initialization events. Pull request courtesy G Allajmi.
Mike Bayer [Fri, 5 Dec 2025 22:41:56 +0000 (17:41 -0500)]
fix / modernize short_selects example
Fixed the "short_selects" performance example where the cache was being
used in all the examples, making it impossible to compare performance with
and without the cache. Less important comparisons like "lambdas" and
"baked queries" have been removed.
Mike Bayer [Wed, 3 Dec 2025 19:48:08 +0000 (14:48 -0500)]
Add a test for #13021
Confirmed the upstream fix for [1] given at [2] solves the issue
illustrated here, this patch adds a test for this case as our
existing tests did not catch this error in python 3.14.1.
Mike Bayer [Mon, 1 Dec 2025 20:11:50 +0000 (15:11 -0500)]
run sentinel server side fns outside of VALUES
Fixed the structure of the SQL string used for the
:ref:`engine_insertmanyvalues` feature when an explicit sequence with
``nextval()`` is used. The SQL function invocation for the sequence has
been moved from being rendered inline within each tuple inside of VALUES to
being rendered once in the SELECT that reads from VALUES. This change
ensures the function is invoked in the correct order as rows are processed,
rather than assuming PostgreSQL will execute inline function calls within
VALUES in a particular order. While current PostgreSQL versions appear to
handle the previous approach correctly, the database does not guarantee
this behavior for future versions.
Yossi [Mon, 1 Dec 2025 17:06:12 +0000 (12:06 -0500)]
[typing] Fix type error when passing Mapped columns to values()
This adjusts the _DMLOnlyColumnArgument type to be a more
focused _OnlyColumnArgument type where we also add a more tightly
focused coercion, while still allowing ORM attributes to be used
as arguments.
Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Closes: #13012
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13012
Pull-request-sha: 5ebb402c686abf1090e5b83e3489dfca4908efdf