3

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!

bryantism
  • 167
  • 9

1 Answers1

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.

Menno Hölscher
  • 567
  • 1
  • 3
  • 9
  • is there a way to get new result without explicit use a session each time? – bryantism Aug 16 '15 at 10:31
  • Just try to change transaction isolation level to READ_COMMITTED http://docs.sqlalchemy.org/en/rel_0_9/dialects/mysql.html#mysql-isolation-level – E.Big May 27 '18 at 11:43