From: Mike Bayer Date: Fri, 4 Feb 2011 22:37:36 +0000 (-0500) Subject: - When explicit sequence execution derives the name X-Git-Tag: rel_0_7b1~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d5cc2f83c1183eb65d0daac3532a1645d0cd9513;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - 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] --- diff --git a/CHANGES b/CHANGES index 7f5ef2f6fd..a848035efd 100644 --- a/CHANGES +++ b/CHANGES @@ -206,6 +206,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 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 4e7e114c9d..22d269cdf0 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -706,14 +706,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, column.type) diff --git a/test/dialect/test_postgresql.py b/test/dialect/test_postgresql.py index f8ca65e603..9aa281979d 100644 --- a/test/dialect/test_postgresql.py +++ b/test/dialect/test_postgresql.py @@ -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()