问题
I have a Model class:
class PlatformUsage(db.Model):
__tablename__ = 'platform_usage'
id = db.Column(db.BigInteger, primary_key=True)
module = db.Column(db.String(64))
rb = db.Column(db.BigInteger)
status = db.Column(db.String(64))
platform = db.Column(db.String(64))
def __init__(self, module, rb, status, platform):
self.module = module
self.rb = rb
self.status = status
self.platform = platform
def __repr__(self):
return "<PlatformUsage(module: %s, rb: %d, status: %s, platform: %s>" % (
self.module, self.rb, self.status, self.platform)
when i query like this:
while True:
PlatformUsage.query.filter_by(module='xxx')
I change the db externally, I can not get the newest results! why ?
session.query(PlatformUsage).filter_by(xxxx)
will get the correct result!
回答1:
The question Mikko Ohtamaa links to doesn't immediately answer your question, but contains what you need to know to understand.
After the first time you execute a query you are in a transaction. Within the transactions most DBMS guarantee you repeatable read (or give you that option). I.e. each time you run a query within a transaction you will get the same answer. That is what is happening when you execute the first code.
On the scond one you probably hit F5 on the browser and you get a new transaction. That gives you the newest data.
来源:https://stackoverflow.com/questions/31986875/why-flask-sqlalchemy-model-query-does-not-get-newest-records