@contextlib.contextmanager def sqlite_disable_foreign_keys(engine): """Temprorary disable foreign keys This context manager allows one to temprorary disable checking of foreign keys in SQLite. This is actually very useful, because we recreate the original table in order to drop constraints or columns, which leads to breaking of existing foreign keys. """ engine.execute('PRAGMA foreign_keys = OFF;') yield engine.execute('PRAGMA foreign_keys = ON;')