diff options
| author | Federico Caselli <cfederico87@gmail.com> | 2022-09-24 15:50:26 +0200 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-10-17 15:36:25 -0400 |
| commit | 974b1bd0fc40e11fc2886b5a9fc333feeeebf546 (patch) | |
| tree | 421f0545c13a203f40435c4646a0de664e0e9756 /lib/sqlalchemy/dialects | |
| parent | 665c94cc2f0340735515c4f4477e11b556d2bcd8 (diff) | |
| download | sqlalchemy-974b1bd0fc40e11fc2886b5a9fc333feeeebf546.tar.gz | |
Revert automatic set of sequence start to 1
The :class:`.Sequence` construct restores itself to the DDL behavior it
had prior to the 1.4 series, where creating a :class:`.Sequence` with
no additional arguments will emit a simple ``CREATE SEQUENCE`` instruction
**without** any additional parameters for "start value". For most backends,
this is how things worked previously in any case; **however**, for
MS SQL Server, the default value on this database is
``-2**63``; to prevent this generally impractical default
from taking effect on SQL Server, the :paramref:`.Sequence.start` parameter
should be provided. As usage of :class:`.Sequence` is unusual
for SQL Server which for many years has standardized on ``IDENTITY``,
it is hoped that this change has minimal impact.
Fixes: #7211
Change-Id: I1207ea10c8cb1528a1519a0fb3581d9621c27b31
Diffstat (limited to 'lib/sqlalchemy/dialects')
| -rw-r--r-- | lib/sqlalchemy/dialects/mssql/base.py | 33 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/mssql/provision.py | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/oracle/base.py | 4 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 8 |
4 files changed, 43 insertions, 10 deletions
diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 085a2c27b..fdb0704f6 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -335,12 +335,31 @@ This is an auxiliary use case suitable for testing and bulk insert scenarios. SEQUENCE support ---------------- -The :class:`.Sequence` object now creates "real" sequences, i.e., -``CREATE SEQUENCE``. To provide compatibility with other dialects, -:class:`.Sequence` defaults to a start value of 1, even though the -T-SQL defaults is -9223372036854775808. +The :class:`.Sequence` object creates "real" sequences, i.e., +``CREATE SEQUENCE``:: -.. versionadded:: 1.4.0 + >>> from sqlalchemy import Sequence + >>> from sqlalchemy.schema import CreateSequence + >>> from sqlalchemy.dialects import mssql + >>> print(CreateSequence(Sequence("my_seq", start=1)).compile(dialect=mssql.dialect())) + CREATE SEQUENCE my_seq START WITH 1 + +For integer primary key generation, SQL Server's ``IDENTITY`` construct should +generally be preferred vs. sequence. + +..tip:: + + The default start value for T-SQL is ``-2**63`` instead of 1 as + in most other SQL databases. Users should explicitly set the + :paramref:`.Sequence.start` to 1 if that's the expected default:: + + seq = Sequence("my_sequence", start=1) + +.. versionadded:: 1.4 added SQL Server support for :class:`.Sequence` + +.. versionchanged:: 2.0 The SQL Server dialect will no longer implicitly + render "START WITH 1" for ``CREATE SEQUENCE``, which was the behavior + first implemented in version 1.4. MAX on VARCHAR / NVARCHAR ------------------------- @@ -2907,7 +2926,9 @@ class MSDialect(default.DefaultDialect): supports_sequences = True sequences_optional = True - # T-SQL's actual default is -9223372036854775808 + # This is actually used for autoincrement, where itentity is used that + # starts with 1. + # for sequences T-SQL's actual default is -9223372036854775808 default_sequence_base = 1 supports_native_boolean = False diff --git a/lib/sqlalchemy/dialects/mssql/provision.py b/lib/sqlalchemy/dialects/mssql/provision.py index f307c7140..a7ecf4aa3 100644 --- a/lib/sqlalchemy/dialects/mssql/provision.py +++ b/lib/sqlalchemy/dialects/mssql/provision.py @@ -14,6 +14,7 @@ from ...testing.provision import drop_all_schema_objects_pre_tables from ...testing.provision import drop_db from ...testing.provision import get_temp_table_name from ...testing.provision import log +from ...testing.provision import normalize_sequence from ...testing.provision import run_reap_dbs from ...testing.provision import temp_table_keyword_args @@ -116,3 +117,10 @@ def drop_all_schema_objects_pre_tables(cfg, eng): ) ) ) + + +@normalize_sequence.for_db("mssql") +def normalize_sequence(cfg, sequence): + if sequence.start is None: + sequence.start = 1 + return sequence diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index c06da6ffe..6481ae483 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -66,14 +66,14 @@ specify sequences, use the sqlalchemy.schema.Sequence object which is passed to a Column construct:: t = Table('mytable', metadata, - Column('id', Integer, Sequence('id_seq'), primary_key=True), + Column('id', Integer, Sequence('id_seq', start=1), primary_key=True), Column(...), ... ) This step is also required when using table reflection, i.e. autoload_with=engine:: t = Table('mytable', metadata, - Column('id', Integer, Sequence('id_seq'), primary_key=True), + Column('id', Integer, Sequence('id_seq', start=1), primary_key=True), autoload_with=engine ) diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index f3db1a95e..5eab8f47c 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -27,9 +27,13 @@ default corresponding to the column. To specify a specific named sequence to be used for primary key generation, use the :func:`~sqlalchemy.schema.Sequence` construct:: - Table('sometable', metadata, - Column('id', Integer, Sequence('some_id_seq'), primary_key=True) + Table( + "sometable", + metadata, + Column( + "id", Integer, Sequence("some_id_seq", start=1), primary_key=True ) + ) When SQLAlchemy issues a single INSERT statement, to fulfill the contract of having the "last insert identifier" available, a RETURNING clause is added to |
