From: Mike Bayer Date: Tue, 6 Apr 2010 22:53:51 +0000 (-0400) Subject: - Postgresql now reflects sequence names associated with X-Git-Tag: rel_0_6_0~42^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a6480a2c31a52c7fcf1cd5487033d61afab8a7ee;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - 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] --- diff --git a/CHANGES b/CHANGES index c12e213a50..eba1026c17 100644 --- 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 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index bef2f1c611..dcdaa3c7b1 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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 diff --git a/test/dialect/test_postgresql.py b/test/dialect/test_postgresql.py index 1d39d56536..fbe62cdec8 100644 --- a/test/dialect/test_postgresql.py +++ b/test/dialect/test_postgresql.py @@ -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),