]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support data types for CREATE SEQUENCE in PostgreSQL
authorFederico Caselli <cfederico87@gmail.com>
Fri, 7 Aug 2020 20:38:24 +0000 (22:38 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Tue, 18 Aug 2020 22:03:28 +0000 (22:03 +0000)
Allow specifying the data type when creating a :class:`.Sequence` in
PostgreSQL by using the parameter :paramref:`.Sequence.data_type`.

Fixes: #5498
Change-Id: I2b4a80aa89b1503c56748dc3ecd2cf145faddd8b

doc/build/changelog/unreleased_14/5498.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_14/5498.rst b/doc/build/changelog/unreleased_14/5498.rst
new file mode 100644 (file)
index 0000000..d3eb848
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: sql, postgresql
+    :tickets: 5498
+
+    Allow specifying the data type when creating a :class:`.Sequence` in
+    PostgreSQL by using the parameter :paramref:`.Sequence.data_type`.
index 7717a2526bf0b428d383c3b97f01b0b027731966..92e48f1f31be354d1d3e00c9ce89997ba0100434 100644 (file)
@@ -2184,6 +2184,17 @@ class PGDDLCompiler(compiler.DDLCompiler):
             generated.sqltext, include_table=False, literal_binds=True
         )
 
+    def visit_create_sequence(self, create, **kw):
+        prefix = None
+        if create.element.data_type is not None:
+            prefix = " AS %s" % self.type_compiler.process(
+                create.element.data_type
+            )
+
+        return super(PGDDLCompiler, self).visit_create_sequence(
+            create, prefix=prefix, **kw
+        )
+
 
 class PGTypeCompiler(compiler.GenericTypeCompiler):
     def visit_TSVECTOR(self, type_, **kw):
index ce285007f75169a27eff6e41987e741b7d117657..708dbe147a10570bf9c1b5b74d1abebbd3cd7a95 100644 (file)
@@ -1,6 +1,7 @@
 # coding: utf-8
 
 from sqlalchemy import and_
+from sqlalchemy import BigInteger
 from sqlalchemy import cast
 from sqlalchemy import Column
 from sqlalchemy import Computed
@@ -15,6 +16,7 @@ from sqlalchemy import null
 from sqlalchemy import schema
 from sqlalchemy import select
 from sqlalchemy import Sequence
+from sqlalchemy import SmallInteger
 from sqlalchemy import String
 from sqlalchemy import Table
 from sqlalchemy import testing
@@ -92,6 +94,20 @@ class SequenceTest(fixtures.TestBase, AssertsCompiledSQL):
                 r = conn.execute(t.insert())
                 eq_(r.inserted_primary_key, (1,))
 
+    @testing.combinations(
+        (None, ""),
+        (Integer, "AS INTEGER "),
+        (SmallInteger, "AS SMALLINT "),
+        (BigInteger, "AS BIGINT "),
+    )
+    def test_create_index_concurrently(self, type_, text):
+        s = Sequence("s1", data_type=type_)
+        self.assert_compile(
+            schema.CreateSequence(s),
+            "CREATE SEQUENCE s1 %sSTART WITH 1" % text,
+            dialect=postgresql.dialect(),
+        )
+
 
 class CompileTest(fixtures.TestBase, AssertsCompiledSQL):