【问题标题】:Sqlalchemy remote database session refresh/flush issueSqlalchemy 远程数据库会话刷新/刷新问题
【发布时间】:2012-03-28 12:44:35
【问题描述】:

我正在开发 python-pyside 桌面应用程序,它使用远程数据库并且应用程序安装在多台机器上,应用程序使用 sql alchemy。

问题是: 应用程序有一个表格打印机,它包含 8 条记录。 假设应用程序在 2 台机器 m1 和 m2 上运行, 如果 m1 用户删除/更新/插入打印机记录,然后如果 m2 获取打印机,则显示 8 台打印机(与启动相同) 如果 m2 在应用程序中执行任何更新/插入/删除操作并尝试从打印机获取记录,则它会显示正确的数据。

我认为这个问题/行为是由于 sql alchemy 会话造成的。

代码:

#config file
self.engine = create_engine ('mysql://user:pwd@remotehost/database')

#Database session file
configuration=Config()         #config consist db,dbusername, 
                               #host name, pwd and engin
engine =configuration.engine 
db_session = scoped_session(sessionmaker(autocommit=False,
                        autoflush=False,
                        bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()

#initialization
import models
Base.metadata.create_all(bind=engine)

#Printer model file
#get query 
printers = db_session.query(Printer).filter(Printer.status != 0)
.order_by(asc(Printer.name)).all()

#alternative get query used/it also have same problem
printers = Printer.query.filter(Printer.status != 0)
.order_by(asc(Printer.name)).all()

会话仅在应用程序的主/启动文件中初始化一次。

请帮帮我。

我尝试了 db_session.flush() 也 autoflush=True,但它不起作用。

【问题讨论】:

  • @Lafada session.commit 在查询后使用,问题是在获取(选择/过滤)不同用户的数据时。
  • 谢谢拉法达!!! get(select/filter) 查询之前的 session.commit 解决了我的问题。

标签: python mysql database sqlalchemy


【解决方案1】:

在获取数据之前使用session.commit 而不是session.flush

【讨论】:

    【解决方案2】:

    谢谢拉法达,

    get(select/filter) 查询之前的 session.commit 解决了我的问题。

    #Printer model file
    db_session.commit()
    #get query 
    printers = db_session.query(Printer).filter(Printer.status != 0)
    .order_by(asc(Printer.name)).all()
    
    #alternative get query used/it also have same problem
    db_session.commit()
    printers = Printer.query.filter(Printer.status != 0)
    .order_by(asc(Printer.name)).all()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多