My application does not update the database - all queries are SELECT
statements. I'm struggling how best to handle direct changes to the database (ie opening MySQLWorkbench and changing data there). Without session.commit()
, my Flask application is returning stale data.
My solution right now is to have a session.commit()
as the first line of each Flask endpoint, but I feel this is the incorrect way of handling this.
Session creation at start of app:
engine = db.create_engine('mysql+pymysql://...')
connection = engine.connect()
metadata = db.MetaData()
Base = declarative_base()
Session = sessionmaker(autoflush=True)
Session.configure(bind=engine)
session = Session()
session.expire_all()
to mark all session data as expired. Then when you are trying to access something, it will be fetched from the database.
session.expire(object)
does the same but for objects only
db.session.refresh(some_object)
expires and reloads all object data
Nice article about that can be found here: https://www.michaelcho.me/article/sqlalchemy-commit-flush-expire-refresh-merge-whats-the-difference
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.