From: Mike Bayer Date: Sun, 10 Apr 2011 14:49:08 +0000 (-0400) Subject: - Fixed bug where reflection of foreign key X-Git-Tag: rel_0_6_7~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ba5240aec039906f0e79d9cb3e2e9323e873fc3b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug where reflection of foreign key created as "REFERENCES " without col name would fail. [ticket:2115] --- diff --git a/CHANGES b/CHANGES index 9a5ea3a371..fcc94ff4b1 100644 --- a/CHANGES +++ b/CHANGES @@ -86,6 +86,11 @@ CHANGES and Pypy. Thanks to Jaimy Azle for spotting this. [ticket:2102] +- sqlite + - Fixed bug where reflection of foreign key + created as "REFERENCES " without + col name would fail. [ticket:2115] + - postgresql - When explicit sequence execution derives the name of the auto-generated sequence of a SERIAL column, diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 01e9d25be3..e2f5f23159 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -654,6 +654,10 @@ class SQLiteDialect(default.DefaultDialect): if row is None: break (constraint_name, rtbl, lcol, rcol) = (row[0], row[2], row[3], row[4]) + # sqlite won't return rcol if the table + # was created with REFERENCES , no col + if rcol is None: + rcol = lcol rtbl = re.sub(r'^\"|\"$', '', rtbl) lcol = re.sub(r'^\"|\"$', '', lcol) rcol = re.sub(r'^\"|\"$', '', rcol) diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index f4871969a4..9a09153033 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -636,3 +636,22 @@ class TestAutoIncrement(TestBase, AssertsCompiledSQL): 'CREATE TABLE noautoinctable (id INTEGER ' 'NOT NULL, x INTEGER, PRIMARY KEY (id))', dialect=sqlite.dialect()) + + +class ReflectHeadlessFKsTest(TestBase): + def setup(self): + testing.db.execute("CREATE TABLE a (id INTEGER PRIMARY KEY)") + testing.db.execute("CREATE TABLE b (id INTEGER PRIMARY KEY REFERENCES a)") + + def teardown(self): + testing.db.execute("drop table b") + testing.db.execute("drop table a") + + def test_reflect_tables_fk_no_colref(self): + meta = MetaData() + a = Table('a', meta, autoload=True, autoload_with=testing.db) + b = Table('b', meta, autoload=True, autoload_with=testing.db) + + assert b.c.id.references(a.c.id) + +