Commit Graph

7424 Commits

Author SHA1 Message Date
G Allajmi 9fe3c3cd30 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.

Fixes: #12867
Closes: #13003
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13003
Pull-request-sha: 1a9216062f

Change-Id: I8c55d8faae25d56ff63c9126d569c01d8ee6c7dd
2025-12-09 15:12:34 -05:00
Mike Bayer 6785a09670 use os.urandom() for CTE, aliased anon id
Fixed issue where anonymous label generation for :class:`.CTE` constructs
could produce name collisions when Python's garbage collector reused memory
addresses during complex query compilation. The anonymous name generation
for :class:`.CTE` and other aliased constructs like :class:`.Alias`,
:class:`.Subquery` and others now use :func:`os.urandom` to generate unique
identifiers instead of relying on object ``id()``, ensuring uniqueness even
in cases of aggressive garbage collection and memory reuse.

Fixes: #12990
Change-Id: If56a53840684bc7d2b7637f1e154dfed1cac5f32
2025-12-09 15:23:29 +00:00
Mike Bayer c7bd25650a fix typo
Change-Id: I5a440f9d3a8bb5afb45b0af4d9f1a112255d5f96
2025-12-08 11:40:34 -05:00
Michael Bayer f9f07ce096 Merge "support monotonic functions as sentinels" into main 2025-12-07 14:16:09 +00:00
Mike Bayer 437745e92e support monotonic functions as sentinels
Added support for monotonic server-side functions such as PostgreSQL 18's
``uuidv7()`` to work with the :ref:`engine_insertmanyvalues` feature.
By passing ``monotonic=True`` to any :class:`.Function`, the function can
be used as a sentinel for tracking row order in batched INSERT operations
with RETURNING, allowing the ORM and Core to efficiently batch INSERT
statements while maintaining deterministic row ordering.

Fixes: #13014
Change-Id: I2fabf96c8fbdb6c1d255fd4781cbd31fed17e1e9
2025-12-07 08:51:13 -05:00
Michael Bayer a1f066b06d Merge "Revert "behave more like dataclasses when creating them"" into main 2025-12-06 15:36:48 +00:00
Michael Bayer 4ff1d604f9 Revert "behave more like dataclasses when creating them"
This reverts commit be4e3e675e.

Reason for revert: everything has passed very well on jenkins, however GH actions is showing the two new test_cpython_142214 tests from 133f14dabe suddenly failing across hundreds of scenarios.  given the risk of this change since dataclasses are very weird, need to see what this is about.   im suspecting point release changes in older pythons like 3.10, 3.11, etc. which seems a bit ominous.

Change-Id: I7c98085b5e3482cad3291194e2ab1f8018db2eff
2025-12-06 15:07:25 +00:00
Michael Bayer 30d80ec1d4 Merge "behave more like dataclasses when creating them" into main 2025-12-06 14:09:35 +00:00
Mike Bayer 66d55b5199 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.

Change-Id: Ia55391ba23e01d2ed136c84f9c34bb16689ce10e
2025-12-05 17:44:57 -05:00
Federico Caselli be4e3e675e behave more like dataclasses when creating them
A change in the mechanics of how Python dataclasses are applied to classes
that use :class:`.MappedAsDataclass` or
:meth:`.registry.mapped_as_dataclass`, to no longer modify the
`__annotations__` collection that's on the class, instead manipulating
regular class-bound attributes in order to satisfy the class requirements
for the dataclass creation function.  This works around an issue that has
appeared in Python 3.14.1, provides for a much simpler implementation, and
also maintains accurate typing information about the attributes as the
dataclass is built.

Fixes: #13021
Change-Id: I6ae13db7f647ad04e202667d69f2b1bb385c032d
2025-12-05 14:13:09 -05:00
Mike Bayer 9a0d004331 filter_by works across multiple entities
The :meth:`_sql.Select.filter_by`, :meth:`_sql.Update.filter_by` and
:meth:`_sql.Delete.filter_by` methods now search across all entities
present in the statement, rather than limiting their search to only the
last joined entity or the first FROM entity. This allows these methods
to locate attributes unambiguously across multiple joined tables,
resolving issues where changing the order of operations such as
:meth:`_sql.Select.with_only_columns` would cause the method to fail.

If an attribute name exists in more than one FROM clause entity, an
:class:`_exc.AmbiguousColumnError` is now raised, indicating that
:meth:`_sql.Select.filter` (or :meth:`_sql.Select.where`) should be used
instead with explicit table-qualified column references.

Fixes: #8601
Change-Id: I6a46b8f4784801f95f7980ca8ef92f1947653572
2025-12-04 13:53:27 +00:00
Michael Bayer 229a71fb0a Merge "send same sub_stmt to after_cursor_execute as before" into main 2025-12-03 19:17:16 +00:00
Michael Bayer 3d64f83e6c Merge "run sentinel server side fns outside of VALUES" into main 2025-12-03 16:56:08 +00:00
Mike Bayer 6924f6c5d9 send same sub_stmt to after_cursor_execute as before
Fixed issue in the :meth:`.ConnectionEvents.after_cursor_execute` method
where the SQL statement and parameter list for an "insertmanyvalues"
operation sent to the event would not be the actual SQL / parameters just
emitted on the cursor, instead being the non-batched form of the statement
that's used as a template to generate the batched statements.

Fixes: #13018
Change-Id: Ib5b6c576f2c7a9ed275de30290d619cc651b4816
2025-12-02 23:10:10 -05:00
Mike Bayer c5d09f5ed4 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.

Fixes: #13015
Change-Id: Ia0a2a4e8f89e21852d7cb550dfa5d9ea9447b590
2025-12-02 22:52:57 -05:00
Mike Bayer d723e235c0 doc updates
Change-Id: I60bccf962e4cd1ed567a772c43c969c4807662f6
2025-12-02 20:06:50 -05:00
Yeongbae Jeon 6baf3b9fb2 Add native BOOLEAN type support for Oracle 23c and later versions
Added support for native BOOLEAN support in Oracle Database 23c and above.
The Oracle dialect now renders ``BOOLEAN`` automatically when
:class:`.Boolean` is used in DDL, and also now supports direct use of the
:class:`.BOOLEAN` datatype, when 23c and above is in use.  For Oracle
versions prior to 23c, boolean values continue to be emulated using
SMALLINT as before.   Special case handling is also present to ensure a
SMALLINT that's interpreted with the :class:`.Boolean` datatype on Oracle
Database 23c and above continues to return bool values. Pull request
courtesy Yeongbae Jeon.

Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #11633
Closes: #13000
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13000
Pull-request-sha: 65b8a2c92f

Change-Id: I13be32f6bad5d918f6c7c9622f0deae78f5c6551
2025-12-02 08:25:33 -05:00
Mike Bayer b9e3cacb0e add TString support
Added support for Python 3.14+ template strings (t-strings) via the new
:func:`_sql.tstring` construct, as defined in :pep:`750`. This feature
allows for ergonomic SQL statement construction by automatically
interpolating Python values and SQLAlchemy expressions within template
strings.

Part of the challenge here is the syntax only works on py314, so we have
to exclude the test file at many levels when py314 is not used.  not
sure yet how i want to adjust pep8 tests and rules for this.

Fixes: #12548
Change-Id: Ia060d1387ff452fe4f5d924f683529a22a8e1f72
2025-11-30 14:38:13 -05:00
Shamil 6ee4d884f0 Fix parameter mutation in orm_pre_session_exec()
The :meth:`_events.SessionEvents.do_orm_execute` event now allows direct
mutation or replacement of the :attr:`.ORMExecuteState.parameters`
dictionary or list, which will take effect when the the statement is
executed.  Previously, changes to this collection were not accommodated by
the event hook.  Pull request courtesy Shamil.

Fixes: #12921
Closes: #12989
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12989
Pull-request-sha: 86b64e0617

Change-Id: I04874b6ca720eb2be1470067ced4afd79896e267
2025-11-20 14:18:35 -05:00
Shamil a057c474bf Fix type hint for with_for_update() to support tuples of table classes
Fixed typing issue where :meth:`.Select.with_for_update` would not support
lists of ORM entities in the :paramref:`.Select.with_for_update.of`
parameter. Pull request courtesy Shamil.

Fixes: #12730
Closes: #12988
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12988
Pull-request-sha: 41a38bfe38

Change-Id: I61d60a4f4d2b16037da8d5f30e33f5d74fa47374
2025-11-19 09:04:04 -05:00
Mike Bayer 42710c9220 propagate _scalar_type() for SelectStatementGrouping
Fixed issue where using the :meth:`.ColumnOperators.in_` operator with a
nested :class:`.CompoundSelect` statement (e.g. an ``INTERSECT`` of
``UNION`` queries) would raise a :class:`NotImplementedError` when the
nested compound select was the first argument to the outer compound select.
The ``_scalar_type()`` internal method now properly handles nested compound
selects.

Fixes: #12987
Change-Id: I6aa1b38863588d371bbac74b3531b99ccd5fcaec
2025-11-18 15:11:05 -05:00
Mike Bayer 5af013026f add highlight for python3 over api doc sections
the pycon+sql highlight seems to take effect for docstrings
now in sphinx 8 or whatever.

Fixes: #12981
Change-Id: Ifab2c66d6adddf3ce5336f244653a336ac0d2ce9
2025-11-16 10:58:27 -05:00
Federico Caselli 850f7ae12e Add DictBundle
Added :class:`_orm.DictBundle` as a subclass of :class:`_orm.Bundle`
that returns ``dict`` objects.

Fixes: #12960
Change-Id: I798fb917779eb95bda575f2809e58c2f6d3c4706
2025-11-15 18:33:07 +00:00
Fredrik Ålund d96c04950d Add Mimer SQL to list of external dialects (#12969) 2025-11-13 00:05:21 +01:00
Michael Bayer 86c4377019 Merge "Typing: fix type of func.coalesce when used with hybrid properties" into main 2025-11-11 21:43:51 +00:00
Yannick PÉROUX d160cb5314 Typing: fix type of func.coalesce when used with hybrid properties
Fixed typing issue where :class:`.coalesce` would not return the correct
return type when a nullable form of that argument were passed, even though
this function is meant to select the non-null entry among possibly null
arguments.  Pull request courtesy Yannick PÉROUX.

Closes: #12963
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12963
Pull-request-sha: 05d0d9784d

Change-Id: Ife83a384ea57faf446c1fdb542df14627348f40f
2025-11-11 16:13:38 -05:00
Michael Bayer ebd118a1a2 Merge "Add DDL association to Table, CreateView support" into main 2025-11-11 14:48:55 +00:00
Michael Bayer 056ebf8f3d Merge "feat: Support MySQL FOR SHARE locking syntax." into main 2025-11-11 01:20:23 +00:00
JetDrag 59afd08a52 feat: Support MySQL FOR SHARE locking syntax.
Added support for MySQL 8.0.1 + ``FOR SHARE`` to be emitted for the
:meth:`.Select.with_for_uddate` method, which offers compatibility with
``NOWAIT`` and ``SKIP LOCKED``.  The new syntax is used only for MySQL when
version 8.0.1 or higher is detected. Pull request courtesy JetDrag.

Fixes: #10134
Closes: #12964
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12964
Pull-request-sha: 03d5e37cfd

Change-Id: Iafb7a24363284edcfeead94a348f50a470a88403
2025-11-10 19:20:54 -05:00
Michael Bayer 964ca95bdf Merge "remove use of LABEL_STYLE_TABLENAME_PLUS_COL outside of Query" into main 2025-11-10 22:56:57 +00:00
Federico Caselli 33264146ae Improve params implementation
Added new implementation for the :meth:`.Select.params` method and that of
similar statements, via a new statement-only
:meth:`.ExecutableStatement.params` method which works more efficiently and
correctly than the previous implementations available from
:class:`.ClauseElement`, by assocating the given parameter dictionary with
the statement overall rather than cloning the statement and rewriting its
bound parameters.  The :meth:`_sql.ClauseElement.params` and
:meth:`_sql.ClauseElement.unique_params` methods, when called on an object
that does not implement :class:`.ExecutableStatement`, will continue to
work the old way of cloning the object, and will emit a deprecation
warning.    This issue both resolves the architectural / performance
concerns of 🎫`7066` and also provides correct ORM compatibility for
functions like :func:`_orm.aliased`, reported by 🎫`12915`.

Fixes: #7066
Change-Id: I6543c7d0f4da3232b3641fb172c24c446f02f52a
2025-11-10 16:50:54 -05:00
Inada Naoki e8fdc99e9c remove use of LABEL_STYLE_TABLENAME_PLUS_COL outside of Query
Changed the query style for ORM queries emitted by :meth:`.Session.get` as
well as many-to-one lazy load queries to use the default labeling style,
:attr:`_sql.SelectLabelStyle.LABEL_STYLE_DISAMBIGUATE_ONLY`, which normally
does not apply labels to columns in a SELECT statement. Previously, the
older style :attr:`_sql.SelectLabelStyle.LABEL_STYLE_TABLENAME_PLUS_COL`
that labels columns as `<tablename>_<columname>` was used for
:meth:`.Session.get` to maintain compatibility with :class:`_orm.Query`.
The change allows the string representation of ORM queries to be less
verbose in all cases outside of legacy :class:`_orm.Query` use. Pull
request courtesy Inada Naoki.

Fixes: #12932
Closes: #12926
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12926
Pull-request-sha: 6738a73f63

Change-Id: I044a54226a4fcade07adc1a3f5f60b4b3e451a1e
2025-11-10 14:57:35 -05:00
Mike Bayer f3fda75e3d changelog updates
Change-Id: I4e108b5a772ec49ae0f07761cadef9bf0f485297
2025-11-10 08:43:41 -05:00
Mike Bayer 828bedbe3a dont fail if imports not set up
this is to help with vscode extensions

Change-Id: I4dc6c07bec8158c82e376b3399d6e0e104360bf2
2025-11-10 08:43:10 -05:00
Mike Bayer f039bb76ed fix docs code example
Change-Id: I7b262725e6eedaeef9ebc0e956cae515b68ed9cd
2025-11-09 14:21:07 -05:00
Mike Bayer faf24bff86 migration notes updates
Change-Id: I11847af15cfec7c937ac99eae6b3fd030e26be6d
2025-11-08 17:44:09 -05:00
Michael Bayer 59f29eef15 Merge "Allow non-integer RANGE in OVER" into main 2025-11-08 19:58:00 +00:00
Michael Bayer 478a3864d6 Merge "support subscript for hstore" into main 2025-11-08 19:55:01 +00:00
Mike Bayer 98361f19a8 Add DDL association to Table, CreateView support
Added support for the SQL ``CREATE VIEW`` statement via the new
:class:`.CreateView` DDL class. The new class allows creating database
views from SELECT statements, with support for options such as
``TEMPORARY``, ``IF NOT EXISTS``, and ``MATERIALIZED`` where supported by
the target database. Views defined with :class:`.CreateView` integrate with
:class:`.MetaData` for automated DDL generation and provide a
:class:`.Table` object for querying.

this alters the CreateTableAs commit:

* adds the ability for Table to be associated with Create and Drop
  DDL constructs
* Adds CreateView variant of CreateTableAs
* Both associate themselves with Table so they take place in
  create_all/create/drop_all/drop

Fixes: #181
Change-Id: If3e568d3d6a6ce19e3d15198c3fbbe06bd847c83
2025-11-08 13:42:15 -05:00
Mike Bayer 1bddbd9e58 add missing changelog for #12954
Change-Id: I9b779230c236884dc58f79c97e65bd6c983b252e
2025-11-04 16:56:34 -05:00
Mike Bayer 7beb71d208 clarify Core / ORM insert parameter behaviors
it seems to have gotten lost in our newer docs that we're looking
at the first dict only for core insert.  add sections to both
INSERT tutorials explaining this difference

references: #12962
Change-Id: Id2e6c7e7db57fba6aa7838d5c3c65dea1939445a
2025-11-04 09:14:54 -05:00
Mike Bayer 143d2a78c7 support subscript for hstore
Added support for PostgreSQL 14+ HSTORE subscripting syntax. When connected
to PostgreSQL 14 or later, HSTORE columns now automatically use the native
subscript notation ``hstore_col['key']`` instead of the arrow operator
``hstore_col -> 'key'`` for both read and write operations. This provides
better compatibility with PostgreSQL's native HSTORE subscripting feature
while maintaining backward compatibility with older PostgreSQL versions.

as part of this change we add a new parameter to custom_op "visit_name"
which allows a custom op to refer to a specific visit method in a
dialect's compiler.

Fixes: #12948
Change-Id: Id98d333fe78e31d9c7679cb2902f1c7e458d6e11
2025-10-31 21:11:49 -04:00
Mike Bayer 4fd9fbe73f ensure util.get_annotations() is used
Fixed issue in Python 3.14 where dataclass transformation would fail when
a mapped class using :class:`.MappedAsDataclass` included a
:func:`.relationship` referencing a class that was not available at
runtime (e.g., within a ``TYPE_CHECKING`` block). This occurred when using
Python 3.14's :pep:`649` deferred annotations feature, which is the
default behavior without a ``from __future__ import annotations``
directive.

Fixes: #12952
Change-Id: I32f0adba00c32f5bf98fe2880dda1b96a7774d07
2025-10-31 12:20:14 -04:00
Mike Bayer e2bda66b51 fix session cursor result tip
in 2.0, we are usually not returning CursorResult for
Session.execute().

References: #12813
Change-Id: I19049b57790b5429ce7890c86e87b93c07a3f1d2
2025-10-29 08:08:03 -04:00
Michael Bayer 3eb9308def Merge "Support for Create Table As" into main 2025-10-25 20:18:32 +00:00
Greg Jarzab 7f5e9e733c Support for Create Table As
Added support for the SQL ``CREATE TABLE ... AS SELECT`` construct via the
new :class:`_sql.CreateTableAs` DDL construct and the
:meth:`_sql.SelectBase.into` method. The new construct allows creating a
table directly from the results of a SELECT statement, with support for
options such as ``TEMPORARY`` and ``IF NOT EXISTS`` where supported by the
target database.  Pull request courtesy Greg Jarzab.

Fixes: #4950
Closes: #12860
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12860
Pull-request-sha: 7de8a109b8

Change-Id: Id9c8e4a3c520ffc61de1e48e331b6220e3d52fc9
2025-10-25 14:24:46 -04:00
Michael Bayer 70041d4c9c Merge "proposal: remove _execute_compiled()" into main 2025-10-24 21:49:22 +00:00
Violet Folino Gallo f9acbeb778 Allow non-integer RANGE in OVER
Updated the :func:`_sql.over` clause to allow non-integer values in
:paramref:`_sql.over.range_` clause. Previously, only integer values
were allowed and any other values would lead to a failure.
To specify a non-integer value, use the new :class:`_sql.FrameClause`
construct along with the new :class:`_sql.FrameClauseType` enum to specify
the frame boundaries.

Fixes #12596.
Closes: #12695
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12695
Pull-request-sha: 8063cec65d

Change-Id: I248a938f6502d72555c005d86791c992822117d4
2025-10-24 23:10:01 +02:00
Michael Bayer 33f93745bc Merge "improve sqlite reflection regex for unusual names/formats" into main 2025-10-24 14:57:33 +00:00
Mike Bayer 189907be93 improve sqlite reflection regex for unusual names/formats
A series of improvements have been made for reflection of CHECK constraints
on SQLite. The reflection logic now correctly handles table names
containing the strings "CHECK" or "CONSTRAINT", properly supports all four
SQLite identifier quoting styles (double quotes, single quotes, brackets,
and backticks) for constraint names, and accurately parses CHECK constraint
expressions containing parentheses within string literals using balanced
parenthesis matching with string context tracking.    Big thanks to
GruzdevAV for new test cases and implementation ideas.

Fixes: #12924
Change-Id: I0390ac334c98e934c7e0353f47c9f43204791af5
2025-10-24 09:54:44 -04:00