]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- When explicit sequence execution derives the name
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 4 Feb 2011 22:37:36 +0000 (17:37 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 4 Feb 2011 22:37:36 +0000 (17:37 -0500)
of the auto-generated sequence of a SERIAL column,
which currently only occurs if implicit_returning=False,
now accommodates if the table + column name is greater
than 63 characters using the same logic Postgresql uses.
[ticket:1083]

CHANGES
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/test_postgresql.py

diff --git a/CHANGES b/CHANGES
index d11bfa5e05f95069c80ecb5d25ddce6450b13f1f..de02aedf11f9394e3a26aac0092a02fd0afc46e1 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -29,6 +29,14 @@ CHANGES
     the extension compiles and runs on Python 2.4.
     [ticket:2023]
 
+- postgresql
+  - When explicit sequence execution derives the name 
+    of the auto-generated sequence of a SERIAL column, 
+    which currently only occurs if implicit_returning=False, 
+    now accommodates if the table + column name is greater 
+    than 63 characters using the same logic Postgresql uses.  
+    [ticket:1083]
+
 0.6.6
 =====
 - orm
index 18725e755778fa8ab6fcf266083d18a863b20cda..431bcf8ae66b215573ebfe226643743099272f56 100644 (file)
@@ -715,14 +715,23 @@ class PGExecutionContext(default.DefaultExecutionContext):
                 # execute the sequence associated with a SERIAL primary 
                 # key column. for non-primary-key SERIAL, the ID just
                 # generates server side.
-                sch = column.table.schema
 
+                try:
+                    seq_name = column._postgresql_seq_name
+                except AttributeError:
+                    tab = column.table.name 
+                    col = column.name 
+                    tab = tab[0:29 + max(0, (29 - len(col)))] 
+                    col = col[0:29 + max(0, (29 - len(tab)))] 
+                    column._postgresql_seq_name = seq_name = "%s_%s_seq" % (tab, col)
+
+                sch = column.table.schema
                 if sch is not None:
-                    exc = "select nextval('\"%s\".\"%s_%s_seq\"')" % \
-                            (sch, column.table.name, column.name)
+                    exc = "select nextval('\"%s\".\"%s\"')" % \
+                            (sch, seq_name)
                 else:
-                    exc = "select nextval('\"%s_%s_seq\"')" % \
-                            (column.table.name, column.name)
+                    exc = "select nextval('\"%s\"')" % \
+                            (seq_name, )
 
                 return self._execute_scalar(exc)
 
index 63b040f97f374d93be95449487b089445aad2712..2c77e4d0b6695c83465de15dcb25039016d1ae22 100644 (file)
@@ -17,7 +17,7 @@ import logging
 
 class SequenceTest(TestBase, AssertsCompiledSQL):
 
-    def test_basic(self):
+    def test_format(self):
         seq = Sequence('my_seq_no_schema')
         dialect = postgresql.PGDialect()
         assert dialect.identifier_preparer.format_sequence(seq) \
@@ -29,6 +29,24 @@ class SequenceTest(TestBase, AssertsCompiledSQL):
         assert dialect.identifier_preparer.format_sequence(seq) \
             == '"Some_Schema"."My_Seq"'
 
+    @testing.only_on('postgresql', 'foo')
+    @testing.provide_metadata
+    def test_reverse_eng_name(self):
+        engine = engines.testing_engine(options=dict(implicit_returning=False))
+        for tname, cname in [
+            ('tb1' * 30, 'abc'),
+            ('tb2', 'abc' * 30),
+            ('tb3' * 30, 'abc' * 30),
+            ('tb4', 'abc'),
+        ]:
+            t = Table(tname[:57],
+                metadata,
+                Column(cname[:57], Integer, primary_key=True)
+            )
+            t.create(engine)
+            r = engine.execute(t.insert())
+            assert r.inserted_primary_key == [1]
+
 class CompileTest(TestBase, AssertsCompiledSQL):
 
     __dialect__ = postgresql.dialect()