]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Postgresql now reflects sequence names associated with
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Apr 2010 22:53:51 +0000 (18:53 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Apr 2010 22:53:51 +0000 (18:53 -0400)
SERIAL columns correctly, after the name of of the sequence
has been changed.  Thanks to Kumar McMillan for the patch.
[ticket:1071]

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

diff --git a/CHANGES b/CHANGES
index c12e213a50b9e3d22df57f3d9c6faad90c7f4e2c..eba1026c17143729cc93421b772678dcf702c0a6 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -79,6 +79,12 @@ CHANGES
   - Further reworked the "mixin" logic in declarative to 
     additionally allow __mapper_args__ as a @classproperty
     on a mixin, such as to dynamically assign polymorphic_identity.
+
+- postgresql
+  - Postgresql now reflects sequence names associated with 
+    SERIAL columns correctly, after the name of of the sequence
+    has been changed.  Thanks to Kumar McMillan for the patch.
+    [ticket:1071]
     
 - oracle
   - Now using cx_oracle output converters so that the
index bef2f1c611df1a8e9c5938d11440f32dbedafc4e..dcdaa3c7b16e7c4a6c03f9b70640b2f4afff7dca 100644 (file)
@@ -814,7 +814,8 @@ class PGDialect(default.DefaultDialect):
         result = connection.execute(
             sql.text(u"SELECT relname FROM pg_class c "
                 "WHERE relkind = 'r' "
-                "AND '%s' = (select nspname from pg_namespace n where n.oid = c.relnamespace) " %
+                "AND '%s' = (select nspname from pg_namespace n "
+                "where n.oid = c.relnamespace) " %
                 current_schema,
                 typemap = {'relname':sqltypes.Unicode}
             )
@@ -832,7 +833,8 @@ class PGDialect(default.DefaultDialect):
         SELECT relname
         FROM pg_class c
         WHERE relkind = 'v'
-          AND '%(schema)s' = (select nspname from pg_namespace n where n.oid = c.relnamespace)
+          AND '%(schema)s' = (select nspname from pg_namespace n 
+          where n.oid = c.relnamespace)
         """ % dict(schema=current_schema)
         # Py3K
         #view_names = [row[0] for row in connection.execute(s)]
@@ -870,7 +872,8 @@ class PGDialect(default.DefaultDialect):
         SQL_COLS = """
             SELECT a.attname,
               pg_catalog.format_type(a.atttypid, a.atttypmod),
-              (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d
+              (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) 
+                FROM pg_catalog.pg_attrdef d
                WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
               AS DEFAULT,
               a.attnotnull, a.attnum, a.attrelid as table_oid
index 1d39d565367505e3d1586286a4414f3ad5e7c10c..fbe62cdec87f5de8c4419fb5d796c2a0cbbc1c9f 100644 (file)
@@ -1069,6 +1069,35 @@ class MiscTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL):
         finally:
             t.drop(checkfirst=True)
 
+    def test_renamed_sequence_reflection(self):
+        m1 = MetaData(testing.db)
+        t = Table('t', m1, 
+            Column('id', Integer, primary_key=True)
+        )
+        m1.create_all()
+        try:
+            m2 = MetaData(testing.db)
+            t2 = Table('t', m2, autoload=True, implicit_returning=False)
+            eq_(t2.c.id.server_default.arg.text, "nextval('t_id_seq'::regclass)")
+            
+            r = t2.insert().execute()
+            eq_(r.inserted_primary_key, [1])
+            
+            testing.db.connect().\
+                            execution_options(autocommit=True).\
+                            execute("alter table t_id_seq rename to foobar_id_seq")
+                            
+            m3 = MetaData(testing.db)
+            t3 = Table('t', m3, autoload=True, implicit_returning=False)
+            eq_(t3.c.id.server_default.arg.text, "nextval('foobar_id_seq'::regclass)")
+
+            r = t3.insert().execute()
+            eq_(r.inserted_primary_key, [2])
+            
+        finally:
+            m1.drop_all()
+        
+        
     def test_distinct_on(self):
         t = Table('mytable', MetaData(testing.db),
                   Column('id', Integer, primary_key=True),