python - SQLAlchemy not behaving correctly after table dropped -
i have test code creates tables , drops them each test case. however, tests fail after first 1 because relying on code uses sa.table()
first , creates new tables if calling method errors nosuchtableerror
. however, error not thrown after tables first dropped, though engine correctly reports not exist, , not created again.
i've reproduced behavior follows:
>>>import sqlalchemy sa >>>from sqlalchemy import * >>>m = metadata()
the tables don't exist, calling sa.table
here errors expected:
>>>t = sa.table('test', m, autoload_with=eng) ... nosuchtableerror: test
but if create table , drop it, sa.table
not error expected:
>>>t = sa.table('test', m, column('t', string(2))) >>>m.create_all(eng) >>>eng.has_table('test') true >>>t = sa.table('test', m, autoload_with=eng) >>>eng.execute('drop table "test" cascade') <sqlalchemy.engine.result.resultproxy @ 0x106a06150> >>>eng.has_table('test') false >>>t = sa.table('test', m, autoload_with=eng)
no error thrown after last call sa.table
, though table not exist.
what need sa.table()
correctly error after tables have been dropped? engine object passing knows tables not exist, there else need do, refreshing/reconnecting somehow, expected behavior?
turns out need refresh metadata object (create new one) each time call sa.table
if expect schema change. solves problem.
Comments
Post a Comment