A failed table autoload hampers subsequent definition of the same table

Issue #289 resolved
Former user created an issue

When an autoload of a non-existing table fails:

>>> from sqlalchemy import *
>>> md = BoundMetaData('sqlite:///test.db')
>>> Table('nonesuch', md, autoload=True)
Traceback (most recent call last):
...
sqlalchemy.exceptions.NoSuchTableError: nonesuch

the table remains in the metadata dict:

>>> md.tables
{'nonesuch': Table('nonesuch',
BoundMetaData(),
schema=None)}

that way, a subsequent definition of the same table fails:

>>> Table('nonesuch', md,
...     Column('id', Integer, primary_key=True))
Traceback (most recent call last):
...
sqlalchemy.exceptions.ArgumentError: Table 'None.nonesuch' is already
defined. specify 'redefine=True' to remap columns, or 'useexisting=True'
to use the existing table

One could use the 'redefine=True' parameter to avoid this error, but I would prefer avoiding it, because in some cases that would actually, and wrongly, redefine a table.

The attached patch deregisters the table, but only when autoloading raises NoSuchTableError, and therefore makes the subsequent definition work.

Comments (4)

  1. Mike Bayer repo owner

    one issue with this is that MySQL is not able to raise NoSuchTableError at the moment; while all the other DB's reflection method uses a "if no rows, then no table" approach, MySQL's "describe" throws an exception. ill make a separate ticket for that.

    anyway this is patched in changeset:1829

  2. Log in to comment