summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Caselli <cfederico87@gmail.com>2020-08-07 22:38:24 +0200
committerFederico Caselli <cfederico87@gmail.com>2020-08-18 22:03:28 +0000
commit8e01a928d9559014413d08855c187b563c35ae72 (patch)
treeb3ba77209c098aa5da8cd18cb95a64c85f5198f0
parent0901190bb440580f0664fe3f6310173762b908e0 (diff)
downloadsqlalchemy-8e01a928d9559014413d08855c187b563c35ae72.tar.gz
Support data types for CREATE SEQUENCE in PostgreSQL
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
-rw-r--r--doc/build/changelog/unreleased_14/5498.rst6
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py11
-rw-r--r--test/dialect/postgresql/test_compiler.py16
3 files changed, 33 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_14/5498.rst b/doc/build/changelog/unreleased_14/5498.rst
new file mode 100644
index 000000000..d3eb84801
--- /dev/null
+++ b/doc/build/changelog/unreleased_14/5498.rst
@@ -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`.
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index 7717a2526..92e48f1f3 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -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):
diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py
index ce285007f..708dbe147 100644
--- a/test/dialect/postgresql/test_compiler.py
+++ b/test/dialect/postgresql/test_compiler.py
@@ -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):