summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2022-06-18 18:59:48 +0000
committerGerrit Code Review <gerrit@ci3.zzzcomputing.com>2022-06-18 18:59:48 +0000
commit84183b19795a8ccdcbf4adbd9951ff3d60d161fe (patch)
tree5566b601ef945142c5b6a590a25ba4fb4da67089
parentc3102b85c40ab4578a0f56ee1e8eee4a6e0aed55 (diff)
parenta134956c4e4564844c33302ddf27a70102fe00a8 (diff)
downloadsqlalchemy-84183b19795a8ccdcbf4adbd9951ff3d60d161fe.tar.gz
Merge "Allow NUMERIC()/DECIMAL() IDENTITY columns" into main
-rw-r--r--doc/build/changelog/unreleased_14/8111.rst11
-rw-r--r--lib/sqlalchemy/sql/schema.py6
-rw-r--r--lib/sqlalchemy/sql/sqltypes.py8
-rw-r--r--lib/sqlalchemy/sql/type_api.py1
-rw-r--r--test/dialect/mssql/test_query.py17
5 files changed, 42 insertions, 1 deletions
diff --git a/doc/build/changelog/unreleased_14/8111.rst b/doc/build/changelog/unreleased_14/8111.rst
new file mode 100644
index 000000000..ac4329702
--- /dev/null
+++ b/doc/build/changelog/unreleased_14/8111.rst
@@ -0,0 +1,11 @@
+.. change::
+ :tags: bug, schema, mssql
+ :tickets: 8111
+
+ Fixed issue where :class:`.Table` objects that made use of IDENTITY columns
+ with a :class:`.Numeric` datatype would produce errors when attempting to
+ reconcile the "autoincrement" column, preventing construction of the
+ :class:`.Column` from using the :paramref:`.Column.autoincrement` parameter
+ as well as emitting errors when attempting to invoke an :class:`.Insert`
+ construct.
+
diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py
index c37b60003..2414d9235 100644
--- a/lib/sqlalchemy/sql/schema.py
+++ b/lib/sqlalchemy/sql/schema.py
@@ -4540,7 +4540,11 @@ class PrimaryKeyConstraint(ColumnCollectionConstraint):
def _autoincrement_column(self) -> Optional[Column[Any]]:
def _validate_autoinc(col: Column[Any], autoinc_true: bool) -> bool:
if col.type._type_affinity is None or not issubclass(
- col.type._type_affinity, type_api.INTEGERTYPE._type_affinity
+ col.type._type_affinity,
+ (
+ type_api.INTEGERTYPE._type_affinity,
+ type_api.NUMERICTYPE._type_affinity,
+ ),
):
if autoinc_true:
raise exc.ArgumentError(
diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py
index b4b444f23..8d796566d 100644
--- a/lib/sqlalchemy/sql/sqltypes.py
+++ b/lib/sqlalchemy/sql/sqltypes.py
@@ -459,6 +459,12 @@ class Numeric(HasExpressionLookup, TypeEngine[_N]):
__visit_name__ = "numeric"
+ if TYPE_CHECKING:
+
+ @util.ro_memoized_property
+ def _type_affinity(self) -> Type[Numeric[_N]]:
+ ...
+
_default_decimal_return_scale = 10
def __init__(
@@ -3611,6 +3617,7 @@ NULLTYPE = NullType()
BOOLEANTYPE = Boolean()
STRINGTYPE = String()
INTEGERTYPE = Integer()
+NUMERICTYPE: Numeric[decimal.Decimal] = Numeric()
MATCHTYPE = MatchType()
TABLEVALUE = TableValueType()
DATETIME_TIMEZONE = DateTime(timezone=True)
@@ -3668,6 +3675,7 @@ type_api.BOOLEANTYPE = BOOLEANTYPE
type_api.STRINGTYPE = STRINGTYPE
type_api.INTEGERTYPE = INTEGERTYPE
type_api.NULLTYPE = NULLTYPE
+type_api.NUMERICTYPE = NUMERICTYPE
type_api.MATCHTYPE = MATCHTYPE
type_api.INDEXABLE = INDEXABLE = Indexable
type_api.TABLEVALUE = TABLEVALUE
diff --git a/lib/sqlalchemy/sql/type_api.py b/lib/sqlalchemy/sql/type_api.py
index 00bae17bc..d8f1e92c4 100644
--- a/lib/sqlalchemy/sql/type_api.py
+++ b/lib/sqlalchemy/sql/type_api.py
@@ -50,6 +50,7 @@ if typing.TYPE_CHECKING:
from .sqltypes import INTEGERTYPE as INTEGERTYPE # noqa: F401
from .sqltypes import MATCHTYPE as MATCHTYPE # noqa: F401
from .sqltypes import NULLTYPE as NULLTYPE
+ from .sqltypes import NUMERICTYPE as NUMERICTYPE # noqa: F401
from .sqltypes import STRINGTYPE as STRINGTYPE # noqa: F401
from .sqltypes import TABLEVALUE as TABLEVALUE # noqa: F401
from ..engine.interfaces import Dialect
diff --git a/test/dialect/mssql/test_query.py b/test/dialect/mssql/test_query.py
index 3576a9fc2..29bf4c812 100644
--- a/test/dialect/mssql/test_query.py
+++ b/test/dialect/mssql/test_query.py
@@ -1,4 +1,6 @@
# -*- encoding: utf-8
+import decimal
+
from sqlalchemy import and_
from sqlalchemy import Column
from sqlalchemy import DDL
@@ -9,6 +11,7 @@ from sqlalchemy import func
from sqlalchemy import Identity
from sqlalchemy import Integer
from sqlalchemy import literal
+from sqlalchemy import Numeric
from sqlalchemy import or_
from sqlalchemy import PrimaryKeyConstraint
from sqlalchemy import select
@@ -39,6 +42,13 @@ class IdentityInsertTest(fixtures.TablesTest, AssertsCompiledSQL):
Column("description", String(50)),
PrimaryKeyConstraint("id", name="PK_cattable"),
)
+ Table(
+ "numeric_identity",
+ metadata,
+ Column("id", Numeric(18, 0), autoincrement=True),
+ Column("description", String(50)),
+ PrimaryKeyConstraint("id", name="PK_numeric_identity"),
+ )
def test_compiled(self):
cattable = self.tables.cattable
@@ -61,6 +71,13 @@ class IdentityInsertTest(fixtures.TablesTest, AssertsCompiledSQL):
lastcat = conn.execute(cattable.select().order_by(desc(cattable.c.id)))
eq_((10, "PHP"), lastcat.first())
+ numeric_identity = self.tables.numeric_identity
+ # for some reason, T-SQL does not like .values(), but this works
+ result = conn.execute(
+ numeric_identity.insert(), dict(description="T-SQL")
+ )
+ eq_(result.inserted_primary_key, (decimal.Decimal("1"),))
+
def test_executemany(self, connection):
conn = connection
cattable = self.tables.cattable