为什么flask sqlalchemy模型查询没有获得最新记录? [英] why flask sqlalchemy model query does not get newest records?

查看:70
本文介绍了为什么flask sqlalchemy模型查询没有获得最新记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Model类:

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)

当我这样查询时:

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) 

将得到正确的结果!

推荐答案

Mikko Ohtamaa链接到的问题不会立即回答您的问题,而是包含您需要了解的内容.

The question Mikko Ohtamaa links to doesn't immediately answer your question, but contains what you need to know to understand.

第一次执行查询后,您正在进行事务处理.在事务中,大多数DBMS保证您可以重复阅读(或给您该选项). IE.每次在事务中运行查询时,您都会得到相同的答案.当您执行第一个代码时,就是这种情况.

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.

在第二个密码上,您可能在浏览器上按了F5键,然后进行了新的交易.这样可以为您提供最新的数据.

On the scond one you probably hit F5 on the browser and you get a new transaction. That gives you the newest data.

这篇关于为什么flask sqlalchemy模型查询没有获得最新记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆