Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why flask sqlalchemy model query does not get newest records?

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!

like image 332
bryantism Avatar asked Mar 24 '26 01:03

bryantism


1 Answers

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.

like image 120
Menno Hölscher Avatar answered Mar 28 '26 02:03

Menno Hölscher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!