作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何设置选项expire_on_commit=False
仅适用于 Flask-SQLAlchemy 中的当前 session ?
我可以在 SQLAlchemy
的初始化时设置选项对象:
db = SQLAlchemy(app, session_options={"expire_on_commit": False})
db.session.expire_on_commit = False
但它似乎没有任何效果。
最佳答案
expire_on_commit
是 sqlalchemy.orm.session.Session
的参数类(class)。
获取 Session
的首选方法例如,是通过 sqlalchemy.orm.session.sessionmaker
类(class)。 sessionmaker
的实例配置了用于创建 Session
的设置实例。例如。:
>>> from sqlalchemy import create_engine
>>> from sqlalchemy.orm import sessionmaker
>>> engine = create_engine('sqlite:///:memory:')
>>> Session = sessionmaker(bind=engine)
>>> type(Session)
<class 'sqlalchemy.orm.session.sessionmaker'>
>>> session = Session()
>>> type(session)
<class 'sqlalchemy.orm.session.Session'>
sessionmaker
实例返回
Session
实例。
sessionmaker
例如,我们得到一个新的
Session
每次都返回实例。
>>> session1 = Session()
>>> session2 = Session()
>>> session1 is session2
False
scoped_session
改变上述行为:
>>> from sqlalchemy.orm import scoped_session
>>> Session = scoped_session(sessionmaker(bind=engine))
>>> type(Session)
<class 'sqlalchemy.orm.scoping.scoped_session'>
>>> session1 = Session()
>>> session2 = Session()
>>> session1 is session2
True
db.session
处理
request
时您正在使用相同的基础 session 。这是与上面相同的示例,但使用
Flask-SQLAlchemy
延期。
>>> type(db.session)
<class 'sqlalchemy.orm.scoping.scoped_session'>
>>> session1 = db.session()
>>> session2 = db.session()
>>> session1 is session2
True
type(db.session)
在此示例中,产生与
type(Session)
完全相同的结果在前面的例子中。
all sessions created by Flask-SQLAlchemy will have the option set to False, instead i want to set it only for one session.
expire_on_commit
。有时在处理请求时不会。
expire_on_commit
离开:
from contextlib import contextmanager
@contextmanager
def no_expire():
s = db.session()
s.expire_on_commit = False
try:
yield
finally:
s.expire_on_commit = True
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16))
SQLAlchemy
是在做:
import logging
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)
db.drop_all()
db.create_all()
names = ('Jane', 'Tarzan')
db.session.add_all([Person(name=n) for n in names])
db.session.commit()
def test_func():
# query the db
people = Person.query.all()
# commit the session
db.session.commit()
# iterate through people accessing name to see if sql is emitted
for person in people:
print(f'Person is {person.name}')
db.session.rollback()
test_func()
INFO:sqlalchemy.engine.base.Engine:BEGIN (implicit)
INFO:sqlalchemy.engine.base.Engine:SELECT person.id AS person_id, person.name AS person_name
FROM person
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:COMMIT
INFO:sqlalchemy.engine.base.Engine:BEGIN (implicit)
INFO:sqlalchemy.engine.base.Engine:SELECT person.id AS person_id, person.name AS person_name
FROM person
WHERE person.id = %(param_1)s
INFO:sqlalchemy.engine.base.Engine:{'param_1': 1}
*****Person is Jane*****
INFO:sqlalchemy.engine.base.Engine:SELECT person.id AS person_id, person.name AS person_name
FROM person
WHERE person.id = %(param_1)s
INFO:sqlalchemy.engine.base.Engine:{'param_1': 2}
*****Person is Tarzan*****
db.session.rollback()
with no_expire():
test_func()
INFO:sqlalchemy.engine.base.Engine:ROLLBACK
INFO:sqlalchemy.engine.base.Engine:BEGIN (implicit)
INFO:sqlalchemy.engine.base.Engine:SELECT person.id AS person_id, person.name AS person_name
FROM person
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:COMMIT
*****Person is Jane*****
*****Person is Tarzan*****
关于python - Flask SQLAlchemy - 仅为当前 session 设置 expire_on_commit=False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51446322/
在 Flask-SQLAlchemy 中,位于: https://pythonhosted.org/Flask-SQLAlchemy/queries.html 您可以通过 db.session 访问您
如何设置选项expire_on_commit=False仅适用于 Flask-SQLAlchemy 中的当前 session ? 我可以在 SQLAlchemy 的初始化时设置选项对象: db = S
我是一名优秀的程序员,十分优秀!