mssql autoload fails for foreign key reference

Issue #1217 resolved
Former user created an issue

When reflecting a MSSQL table with a foreign key, the referenced table fails to load with the error:

sqlalchemy.exc.NoSuchTableError: [referenced_table](referenced_table)

For this case, I'm using: SA 0.5 RC2, Python 2.5, UnixODBC 2.2.11, tdsodbc 0.63-3.2

The test case uses schema names. Reflection will work with t1, but not with t2 I assume because of the foreign keys. I tried this also without schemas and it wouldn't reflect either table.

import sqlalchemy as sa
engine = sa.create_engine('mssql://somebody:test@somehost/test')
t1_query = "create table somebody.t1 (id int primary key, name char(10))"
t2_query = """
create table somebody.t2 (id int primary key, name char(10),
t1_id int references somebody.t1(id))
"""
engine.execute(t1_query)
engine.execute(t2_query)

try:
    meta = sa.MetaData(bind=engine)
    ##t1 = sa.Table('t1', meta, schema='somebody', autoload=True)
    ##print t1.columns.keys()
    t2 = sa.Table('t2', meta, schema='somebody', autoload=True)
    print t2
finally:
    engine.execute('drop table somebody.t2')
    engine.execute('drop table somebody.t1')

# Fails with:

# sqlalchemy.exc.NoSuchTableError: t1

This may be related to #1012

Comments (2)

  1. Log in to comment