121 Commits

Author SHA1 Message Date
Federico Caselli b0b74dc965 Improve typing story for core from clauses.
Most :class:`_sql.FromClause` subclasses are not generic on
:class:`.TypedColumns` subclasses, that can be used to type their
:attr:`_sql.FromClause.c` collection.
This applied to :class:`_schema.Table`, :class:`_sql.Join`,
:class:`_sql.Subquery`, :class:`_sql.CTE` and more.

Fixes: #13085
Change-Id: I724aca887a85c4a401df875903eda12125066680
2026-03-12 23:02:30 +01:00
Imad Saddik d1026e788c docs: fix grammar in reflection documentation 2026-01-20 21:52:24 +01:00
Mike Bayer 12ccef924a 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 🎫`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.

Fixes: #13067
Change-Id: I2e0d0f45ebb820d2a8f214550f1d1a500bae223b
2026-01-06 21:08:26 -05:00
Mike Bayer 3988071d0d update default driver for oracle, postgresql
The default DBAPI driver for the Oracle Database dialect has been changed
to ``oracledb`` instead of ``cx_oracle``. The ``cx_oracle`` driver remains
fully supported and can be explicitly specified in the connection URL
using ``oracle+cx_oracle://``.

The default DBAPI driver for the PostgreSQL dialect has been changed to
``psycopg`` (psycopg version 3) instead of ``psycopg2``. The ``psycopg2``
driver remains fully supported and can be explicitly specified in the
connection URL using ``postgresql+psycopg2://``.

Fixes: #13010
Change-Id: Ie75810f4c3af609d20da63289d2662dfa2385ca2
2025-12-15 23:01:12 -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 ebd118a1a2 Merge "Add DDL association to Table, CreateView support" into main 2025-11-11 14:48:55 +00: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 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 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 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
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
Mike Bayer 9a6854223c fix tests for aggregate_order_by
forgot this doesnt work on sqlite older than 3.44

Change-Id: Icebc2ffaceb078a436a8f61ba583bc05b77f680d
2025-09-19 15:18:39 -04:00
Reuven Starodubski 459ebc668a Add FunctionElement.aggregate_order_by
Added new generalized aggregate function ordering to functions via the
:func:`_functions.FunctionElement.aggregate_order_by` method, which
receives an expression and generates the appropriate embedded "ORDER BY" or
"WITHIN GROUP (ORDER BY)" phrase depending on backend database.  This new
function supersedes the use of the PostgreSQL
:func:`_postgresql.aggregate_order_by` function, which remains present for
backward compatibility.   To complement the new parameter, the
:paramref:`_functions.aggregate_strings.order_by` which adds ORDER BY
capability to the :class:`_functions.aggregate_strings` dialect-agnostic
function which works for all included backends. Thanks much to Reuven
Starodubski with help on this patch.

Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #12853
Closes: #12856
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12856
Pull-request-sha: d93fb591751227eb1f96052ea3ad449f511f70b3

Change-Id: I8eb41ff2d57695963a358b5f0017ca9372f15f70
2025-09-19 09:38:57 -04:00
Mike Bayer d37d56c1ad Fix PostgreSQL JSONB subscripting regression with functions
Fixed regression in PostgreSQL dialect where JSONB subscription syntax
would generate incorrect SQL for JSONB-returning functions, causing syntax
errors. The dialect now properly wraps function calls and expressions in
parentheses when using the ``[]`` subscription syntax, generating
``(function_call)[index]`` instead of ``function_call[index]`` to comply
with PostgreSQL syntax requirements.

Fixes: #12778
Change-Id: If1238457e6bba6a933023b26519a41aa5de4dbcd
2025-08-04 15:06:47 -04:00
Mike Bayer 5b5db1f5f3 note that SQL parenthesis are based on precedence
References: #12708
Change-Id: I2401e92c936eb01a64ad6896a86faec1c205bc08
2025-07-02 09:18:28 -04:00
Federico Caselli fb5e64c51a add example to update from that uses values
Change-Id: Ic02a722be9a30851a87e0da4759c728e86fb22c8
References: #11768
2025-06-24 17:43:43 -04:00
Christopher Jones 855d03cc15 Update Oracle dialect doc, mostly to prefer python-oracledb
### Description

Small updates for Oracle Database dialect documentation.

- prefer python-oracledb over cx_Oracle
- Prefer the product name 'Oracle Database' over the company name 'Oracle'
- update links
- modernize

This is a refresh of existing content.

I decided the apparently now duplicated sections between cx_Oracle and python-oracledb were justified for clarity due to the inevitable differences.

This pull request is:

- [x] A documentation / typographical / small typing error fix
	- Good to go, no issue or tests are needed

**Have a nice day!**

Closes: #12078
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12078
Pull-request-sha: 7c4dcf9403

Change-Id: I3678976f5524ee164dc31b3122b224ae37060b71
2024-11-12 17:28:38 -05:00
Mike Bayer e9d3e49601 ensure intro to "functions have types" is not misleading
make sure it's clear that there is normally not a return type
for SQL functions

Fixes: #11578
Change-Id: Ia0b66e7fe685dad427822345dd232eb47a0fc44f
2024-07-06 12:24:51 -04:00
lonkeknol 82d14a7515 Docs: simplify language in getting a connection & committing changes
### Description
Simplifies language use in [Getting a Connection](https://docs.sqlalchemy.org/en/20/tutorial/dbapi_transactions.html#getting-a-connection) and [Committing Changes](https://docs.sqlalchemy.org/en/20/tutorial/dbapi_transactions.html#committing-changes)

### Checklist
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.

**Have a nice day!**

Closes: #11542
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/11542
Pull-request-sha: d706e69fb6

Change-Id: I7788f2a16a5127b3c9623f7b00f06f649b04e0fb
2024-06-28 12:27:33 -04:00
lonkeknol fc2cb4496d Docs: simplify language use for "Working with Transactions and the DBAPI"
### Description
This is my first pull request to sqlalchemy. It changes the writing style of two paragraphs in the unified tutorial [here](https://docs.sqlalchemy.org/en/20/tutorial/dbapi_transactions.html#working-with-transactions-and-the-dbapi). My goals were to.
1. Make them easier to read
2. Not change the meaning of the text.
3. Get feedback on whether this type of contribution is considered useful for sqlalchemy.

If this is a useful type of contribution, it might be good to discuss some general guidelines for me to adhere to as I continue. For instance:
- Prefer using present simple tense
- Remove superfluous words where possible
- Keep the pull requests to one or two h2 sections at a time, to make the review easier

### Checklist
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.

I'm curious to hear what you all think. **Have a nice day!**

Closes: #11541
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/11541
Pull-request-sha: 3179690e6a

Change-Id: I9b47f6ce4fd00c44c4b0e19957acf250f5e46d2f
2024-06-27 09:03:30 -04:00
Andreas Deininger 1f6a129d80 Fixing typos (#11105) 2024-03-11 22:40:14 +01:00
acceptacross 64b661d705 chore: remove repetitive words (#11134)
Signed-off-by: acceptacross <csqcqs@gmail.com>
2024-03-11 22:01:42 +01:00
Federico Caselli f0537442eb Add support for preserve_rowcount execution_option
Added new core execution option
paramref:`_engine.Connection.execution_options.preserve_rowcount`
to unconditionally save the ``rowcount`` attribute from the cursor in the
class:`_engine.Result` returned from an execution, regardless of the
statement being executed.
When this option is provided the correct value is also set when
an INSERT makes use of the "insertmanyvalues" mode, that may use
more than one actualy cursor execution.

Fixes: #10974
Change-Id: Icecef6b7539be9f0a1a02b9539864f5f163dcfbc
2024-03-03 17:58:12 -05:00
Federico Caselli 1e099d1a85 clarify example in orm tutorial
Change-Id: Ib6f9a7ce0beacda43ccd6d3c7750778ed3333b38
2024-02-14 20:55:57 +01:00
Umer Zia 3a4e9063e4 Improve formatting of data_select.rst (#10931) 2024-02-06 18:56:09 +01:00
mike bayer 34802a204f Merge "2.1 setup / initial dependency/min version change" into main 2023-11-06 23:03:44 +00:00
Sebastián Ramírez e00215cfcd Fix syntax typo in doc/build/tutorial/data_select.rst (#10590) 2023-11-06 21:01:51 +01:00
Mike Bayer e93a5e890d 2.1 setup / initial dependency/min version change
this includes setup.cfg changes for asyncio and
3.8 min support.  it doesnt have any code changes in support
of these adjusments.

Fixes: #10197
Fixes: #10357

Change-Id: Ic4569c770d1b893a067a9a5dfe13a6e28aaf47fa
2023-11-06 14:10:32 -05:00
AmirSoroush 18d6b50b65 Doc: Update doc/build/tutorial/data_select.rst (#10506)
Removed an extra "that" in a sentence.
2023-10-19 18:44:26 +02:00
Eric Hanchrow 0df9759b73 Revise / rewrite sentences that use the phrase "referred towards"
Comments by Mike <mike_mp@zzzcomputing.com>:

"Referred towards" is not correct English and can be replaced directly
with "referred to".  However, this then introduces a dangling
preposition to sentences which I don't think is appropriate for this
style of writing.  So instead, use phrases like "known as",
"references", "to which X refers".

To help me identify dangling prepositions I made use of ChatGPT,
here's the log of how that transpired:
https://chat.openai.com/share/60d42ff4-c1ac-4232-893a-415c2b6d7320

Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Closes: #10210
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10210
Pull-request-sha: 5d30e79c14

Change-Id: Ib6a98e5be441f5b25d3929a2efb7d873bcfef98e
2023-09-20 18:18:29 -04:00
Mike Bayer b38702ee9d fix typo
Change-Id: I55855508fdf7e0f0af346f4f1f79292d83784aea
2023-08-04 10:01:07 -04:00
Federico Caselli e99fc16fbd fix typo in sphinx target introduced in bd290c4329
Change-Id: Ie085696fca3ce44ad7af611246fc8be45abbc433
2023-06-13 21:19:51 +02:00
Federico Caselli bd290c4329 various minor docs improvements
Change-Id: I5e7332047c03260792bf990aafd0ef4e15f5f082
2023-06-12 22:28:25 +02:00
mike bayer c84b3bf198 Merge "add deterministic imv returning ordering using sentinel columns" into main 2023-04-21 16:51:19 +00:00
Mike Bayer cf6872d3bd add deterministic imv returning ordering using sentinel columns
Repaired a major shortcoming which was identified in the
:ref:`engine_insertmanyvalues` performance optimization feature first
introduced in the 2.0 series. This was a continuation of the change in
2.0.9 which disabled the SQL Server version of the feature due to a
reliance in the ORM on apparent row ordering that is not guaranteed to take
place. The fix applies new logic to all "insertmanyvalues" operations,
which takes effect when a new parameter
:paramref:`_dml.Insert.returning.sort_by_parameter_order` on the
:meth:`_dml.Insert.returning` or :meth:`_dml.UpdateBase.return_defaults`
methods, that through a combination of alternate SQL forms, direct
correspondence of client side parameters, and in some cases downgrading to
running row-at-a-time, will apply sorting to each batch of returned rows
using correspondence to primary key or other unique values in each row
which can be correlated to the input data.

Performance impact is expected to be minimal as nearly all common primary
key scenarios are suitable for parameter-ordered batching to be
achieved for all backends other than SQLite, while "row-at-a-time"
mode operates with a bare minimum of Python overhead compared to the very
heavyweight approaches used in the 1.x series. For SQLite, there is no
difference in performance when "row-at-a-time" mode is used.

It's anticipated that with an efficient "row-at-a-time" INSERT with
RETURNING batching capability, the "insertmanyvalues" feature can be later
be more easily generalized to third party backends that include RETURNING
support but not necessarily easy ways to guarantee a correspondence
with parameter order.

Fixes: #9618
References: #9603
Change-Id: I1d79353f5f19638f752936ba1c35e4dc235a8b7c
2023-04-21 11:30:40 -04:00
Tim Stewart bf3a395e8f fix typo ('resemblence' -> 'resemblance') (#9674) 2023-04-20 19:42:02 +02:00
Chris 24587f2009 Update data_select.rst (#9616)
Fix literal text in example
2023-04-07 22:07:54 +02:00
Tom Wolfskämpf c7ce7ff022 docs: 📚 fix duplicate word typo (#9539) 2023-03-25 18:45:37 +01:00
Mike Bayer 42b3b80fae add tip that reflection is not necessary for an existing database
a new user spent many days misled by this paragraph thinking
they were required to use reflection for an existing database.

Change-Id: I4c6757b931481db7a8d4202334382143e1491935
2023-03-21 13:40:36 -04:00
Mike Bayer 8d5986fafd additional consistency for ORM/Core in tutorial
* Make sure we have blue borders for all sections
* rewrite "blue border" text, refer to textual means of determining
  subject matter for a section; "blue borders" are not a primary
  source of information
* Add some more intro text that was missing

Change-Id: I4d599e13d23bad8bb3c199a11afb53e3e9100c59
References: #9450
2023-03-08 10:10:14 -05:00
Viicos 92159a1728 Remove duplicate word in tutorial (#9420) 2023-03-03 22:46:49 +01:00
easy_markie_tee 9d9e47fe93 Fix Typo In Tutorial (#9399)
Missing a word on the page `Working with Database Metadata`.
First paragraph under section 'Setting up MetaData with Table objects'.
"...the database which we query from is know [as] a table."

Co-authored-by: markie tee <cassette.head@gmail.com>
2023-03-01 22:51:43 +01:00
Harry Lees 961f076230 Unify doc typing
### Description
<!-- Describe your changes in detail -->

Fixes #9168

This PR replaces common occurrences of [PEP 585](https://peps.python.org/pep-0585/) style type annotations with annotations compatible with older versions of Python.

I searched for instances of the following supported types from the PEP and replaced with their legacy typing couterparts.

* tuple # typing.Tuple
* list # typing.List
* dict # typing.Dict
* set # typing.Set
* frozenset # typing.FrozenSet
* type # typing.Type

```
grep -r "list\[.*\]" ./build --exclude-dir="./build/venv/*" --exclude-dir="./build/output/*" --exclude="changelog_[0-9]*\.rst"
```

I excluded changelog files from being altered, I think some of these could be changed if necessary but others are likely to require manual checking as the change may target the new typing style specifically.

For any examples that included imports, I tried to ensure that the correct typing imports were included and properly ordered.

### 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 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.

Closes: #9198
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/9198
Pull-request-sha: 05ad4651b5

Change-Id: I41b93b3dee85f9fe00cfbb3d3eb011212795de29
2023-01-31 19:45:48 +00:00
Mike Bayer e82a5f19e1 reword INSERT explanation
Change-Id: I08460f0a77131c8c1406c3496e9d64a5a26bf6ff
2023-01-19 09:43:19 -05:00
Federico Caselli 8b4b26e709 avoid confusion in rst comment
Change-Id: I6e971f7445ae19f73097516b58776ab05a5371f1
2023-01-18 23:02:48 +01:00
Mike Bayer 6da2f72b11 super-fine pass through the metadata tutorial
try to keep the wordiness down here, using sidebars
and topics for non-essential information.   Sphinx seems
to read out attrs from under TYPE_CHECKING sections now
so link out the attrs in DeclarativeBase w/ docstrings,
not sure why we didn't think of this earlier.  looks great

Change-Id: Ib2e07e3606185998561c2d77b2564fd3eddb4d75
2023-01-15 13:11:38 -05:00
Mike Bayer 48eed9eb9c fix orm-header for standalone
Change-Id: I20e7f8ad5800c2ce5cdc9645c4dbe66f53b8cdf8
2023-01-15 11:28:49 -05:00
Mike Bayer 0c5faf37c2 fix typo
Change-Id: I273459175a3c55290b1f6c28f656ed9c4db17ccf
2023-01-13 14:31:28 -05:00
Federico Caselli b04b7527ed Make the custom type map more discoverable
Change-Id: Id6cdaddad83aa93508e256e54010a6c53218b717
2023-01-13 10:40:19 -05:00
Federico Caselli 06d584ad4b Fixes related to improved sql formatting
Follow up of I07b72e6620bb64e329d6b641afa27631e91c4f16

Change-Id: I1f61974bf9cdc3da5317e546d4f9b649c2029e4d
2023-01-12 23:03:03 +01:00