Flask-SQLAlchemy的隔离级别 [英] Isolation level with Flask-SQLAlchemy

查看:135
本文介绍了Flask-SQLAlchemy的隔离级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解数据库隔离级别如何与Flask-SQLAlchemy配合使用时遇到麻烦,尤其是如何真正提交更改或关闭会话.这是我的问题的背景:

I'm having trouble understanding how database isolation levels work with Flask-SQLAlchemy, and especially how to really commit changes or close a session. Here is the context of my problem :

我正在将Flask-SQLAlchemy用于带有MySQL数据库的Flask项目.这是我的项目的配置方式

I'm using Flask-SQLAlchemy for a Flask project with a MySQL database. Here is how is configured my project

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://user:passwd@localhost/mydb'
SQLALCHEMY_MIGRATE_REPO = '/path/to/myapp/db_repository'

CSRF_ENABLED = True
SECRET_KEY = 'this is a secret'

在我的 __ init __.py 文件中创建 db 对象:

The creation of the db object in my __init__.py file:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import config

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

我已经定义了诸如 Printer 之类的模型:

I have defined models such as Printer one:

from myapp import db
...
class Printer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(120))

    def __init__(self, name):
        self.name = name

我尝试在python终端上玩耍,即使我了解了SQLAlchemy的 Read commit 隔离级别,我仍然遇到以下问题.这是控制台中的内容:

I tried to play around with a python terminal, and even though I read about the Read committed isolation level of SQLAlchemy, I face the following problem. Here is what I have in my console:

>>> from myapp import app
>>> from myapp.models import Printer
>>> import config
>>> from flask.ext.sqlalchemy import SQLAlchemy
>>> app.config.from_object('config')
>>> db = SQLAlchemy(app)
>>> for printer in Printer.query.all():
...     print printer.name
...
Circle
Mww
>>> p = Printer('dummy')
>>> db.session.add(p)
>>> db.session.commit()
>>> for printer in Printer.query.all():
...     print printer.name
...
Circle
Mww
>>> 

当我查询数据库时,我的更改已经提交:

When I look up the database, my change has been committed :

mysql> SELECT * FROM printer;
+----+--------+
| id | name   |
+----+--------+
|  1 | Circle |
|  2 | Mww    |
|  3 | dummy  |
+----+--------+
3 rows in set (0.00 sec)

如果我退出python终端,请再次打开它,然后使用Printer.query.all()读取结果,我的更改就会出现.

If I quit my python terminal, open it again and just read the results with Printer.query.all(), my changes appear.

尽管我了解SQLAlchemy等待更改提交和/或会话关闭,但我不明白为什么在 db.session.commit()语句或如何关闭会话(我尝试过 db.session.close(),在此之后读取数据库不会产生更好的结果)

Though I understand SQLAlchemy waits for change to be committed and/or the session to be closed, I don't get why I can't read my changes after the db.session.commit() statement nor how to close the session (I tried db.session.close(), reading the database after that does not give better results)

感谢您的帮助!

推荐答案

如果使用查询的SQLAlchemy版本会发生什么?

What happens if you use the SQLAlchemy version of the query?

db.session.query(Printer).all() 

我想知道是否正在进行两次会议:

I'm wondering if there are two sessions going on:

  1. 您的应用设置与 Printer.query.all()进行通话的那个人
  2. 您通过该 db = SQLAlchemy()调用创建的摘要中使用的那个
  1. The one your app sets up that Printer.query.all() is talking to
  2. The one you're using in that snippet that you created by that db = SQLAlchemy() call

这将解释为什么 db.session.commit()不会刷新 Printer.query.all()拥有的数据.

It would explain why the db.session.commit() isn't flushing the data that Printer.query.all() has.

这篇关于Flask-SQLAlchemy的隔离级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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