]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add ``Table.autoincrement_column``
authorFederico Caselli <cfederico87@gmail.com>
Fri, 10 Feb 2023 20:37:20 +0000 (21:37 +0100)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 14 Feb 2023 19:45:49 +0000 (14:45 -0500)
Added public property :attr:`_sql.Table.autoincrement_column` that
returns the column identified as autoincrementing in the column.

Fixes: #9277
Change-Id: If60d6f92e0df94f57d00ff6d89d285c61b02f5a4

doc/build/changelog/unreleased_20/9277.rst [new file with mode: 0644]
lib/sqlalchemy/sql/schema.py
test/sql/test_defaults.py

diff --git a/doc/build/changelog/unreleased_20/9277.rst b/doc/build/changelog/unreleased_20/9277.rst
new file mode 100644 (file)
index 0000000..b73649a
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: sql
+    :tickets: 9277
+
+    Added public property :attr:`_sql.Table.autoincrement_column` that
+    returns the column identified as autoincrementing in the column.
index 976432721c2719dff5c4a4ce542c63727a128da7..2a713fea63ab922758d6986067673410e8124a0b 100644 (file)
@@ -1001,9 +1001,31 @@ class Table(
         pass
 
     @util.ro_non_memoized_property
-    def _autoincrement_column(self) -> Optional[Column[Any]]:
+    def _autoincrement_column(self) -> Optional[Column[int]]:
         return self.primary_key._autoincrement_column
 
+    @property
+    def autoincrement_column(self) -> Optional[Column[int]]:
+        """Returns the :class:`.Column` object which currently represents
+        the "auto increment" column, if any, else returns None.
+
+        This is based on the rules for :class:`.Column` as defined by the
+        :paramref:`.Column.autoincrement` parameter, which generally means the
+        column within a single integer column primary key constraint that is
+        not constrained by a foreign key.   If the table does not have such
+        a primary key constraint, then there's no "autoincrement" column.
+        A :class:`.Table` may have only one column defined as the
+        "autoincrement" column.
+
+        .. versionadded:: 2.0.4
+
+        .. seealso::
+
+            :paramref:`.Column.autoincrement`
+
+        """
+        return self._autoincrement_column
+
     @property
     def key(self) -> str:
         """Return the 'key' for this :class:`_schema.Table`.
@@ -4756,7 +4778,7 @@ class PrimaryKeyConstraint(ColumnCollectionConstraint):
             return list(self._columns)
 
     @util.ro_memoized_property
-    def _autoincrement_column(self) -> Optional[Column[Any]]:
+    def _autoincrement_column(self) -> Optional[Column[int]]:
         def _validate_autoinc(col: Column[Any], autoinc_true: bool) -> bool:
             if col.type._type_affinity is None or not issubclass(
                 col.type._type_affinity,
index cc7daf4017927a09026493f80b6a2d5d1b343428..e52499ab48a468d95dab782d1d4c8df8ea419c99 100644 (file)
@@ -1111,6 +1111,7 @@ class AutoIncrementTest(fixtures.TestBase):
         id_ = r.inserted_primary_key[0]
         eq_(id_, 1)
         eq_(connection.scalar(sa.select(single.c.id)), 1)
+        assert single.autoincrement_column is single.c.id
 
     def test_autoinc_detection_no_affinity(self):
         class MyType(TypeDecorator):
@@ -1120,6 +1121,7 @@ class AutoIncrementTest(fixtures.TestBase):
         assert MyType()._type_affinity is None
         t = Table("x", MetaData(), Column("id", MyType(), primary_key=True))
         assert t._autoincrement_column is None
+        assert t.autoincrement_column is None
 
     def test_autoincrement_ignore_fk(self):
         m = MetaData()