SQLAlchemy Session.Refresh不刷新对象 [英] Sqlalchemy session.refresh does not refresh object
本文介绍了SQLAlchemy Session.Refresh不刷新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下映射(直接来自SA示例):
I have the following mapping (straight from SA examples):
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
我正在使用MySql数据库,并且该表具有innoDB引擎.
I'm working with a MySql DB and the table has an innoDB engine.
我的表中有一条记录: 1 |'user1'|'user1 test'|'password'
I have a single record in my table: 1|'user1'|'user1 test'|'password'
我用以下代码打开了一个会话:
I've opened a session with the following code:
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.engine import create_engine
from sqlalchemy.orm.scoping import scoped_session
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
db_engine = create_engine('mysql://...@localhost/test_db?charset=utf8',echo=False,pool_recycle=1800)
session_factory = sessionmaker(bind=db_engine,autocommit=False,autoflush=False)
session_maker = scoped_session(session_factory)
session = session_maker()
user_1 = session.query(User).filter(User.id==1).one()
user_1.name # This prints: u'user1'
现在,当我将数据库中的记录名称更改为'user1_change'并提交它,然后像这样刷新对象时:
Now, when I change the record's name in the DB to 'user1_change' and commit it and then refresh the object like this:
session.refresh(user_1)
user_1.name # This still prints: u'user1' and not u'user1_change'
它仍然打印:u'user1',而不是u'user1_change'.
It still prints: u'user1' and not u'user1_change'.
我在这里想念什么(或设置错误)?
What am I missing (or setting up wrong) here?
谢谢!
推荐答案
查看全文