why flask sqlalchemy model query does not get newest records?

我与影子孤独终老i 提交于 2019-12-11 02:32:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!