Commits

Mike Bayer committed e3da5cb

- Fixed bug where reflection of foreign key
created as "REFERENCES <tablename>" without
col name would fail. [ticket:2115]

Comments (0)

Files changed (3)

     and Pypy.  Thanks to Jaimy Azle for spotting
     this.  [ticket:2102]
 
+- sqlite
+  - Fixed bug where reflection of foreign key
+    created as "REFERENCES <tablename>" without
+    col name would fail.  [ticket:2115]
+
 - postgresql
   - When explicit sequence execution derives the name 
     of the auto-generated sequence of a SERIAL column, 

lib/sqlalchemy/dialects/sqlite/base.py

             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 <tablename>, no col
+            if rcol is None:
+                rcol = lcol
             rtbl = re.sub(r'^\"|\"$', '', rtbl)
             lcol = re.sub(r'^\"|\"$', '', lcol)
             rcol = re.sub(r'^\"|\"$', '', rcol)

test/dialect/test_sqlite.py

                             '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)
+
+