简体   繁体   中英

Clear sqlalchemy reflection cache

I'm using sqlalchemy's reflection tools to get a Table object. I do this because these tables are dynamic and tables/columns can change. Here's the code I'm using:

def getTableByReflection(self, tableName, metadata, engine):

    return Table(tableName, metadata, autoload = True, autoload_with = engine)

The problem is that when the above code is run twice it seems to return the same results regardless of whether or not the columns have changed. I have tried refreshing using the mysession.refresh(mytable) but that fails because the table is not attached to any metadata - which makes sense but then why am I seeing cached results?

Is there any way to tell the metadata/engine/session to forget about this table and let me load it cleanly?

传入新创建的新元数据实例。

With thanks to codeape 's comment above I was able to fix the problem by changing the syntax to:

def getTableByReflection(self, tableName, metadata, engine):

    return Table(tableName, MetaData(), autoload = True, autoload_with = engine)

So passing in a new MetaData() instance each time. This probably affects performance but it's ok for me in this part of my app.

All credit to codeape

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM