【发布时间】:2013-09-09 02:16:23
【问题描述】:
如果我这样做,Sqlalchemy 会跟踪所有更改并提交到数据库:
b = this_session.query(BaseUrl).filter_by(id=value).first()
b.basevalue = "new_value" #changing
this_session.commit() #commit the change to the DB
效果很好。
但是,如果我使用这个:
proxy = this_session.execute("select * from base_url where id = {0}".format(value))
b = create_base_url(proxy) #create an instance of BaseUrl from proxy!
b.basevalue = "new_value" #changing
this_session.commit() #it does NOT commit the change to the DB
当然,create_base_url() 的问题。在这个函数中,我只是通过传递从proxy 获取的各种参数并返回它来创建BaseUrl 的实例。为了让this_session 跟踪对象中的所有更改,我没有做任何其他事情。我需要这样的功能:
this_session.attach(b) #keep track of changes in b
以便跟踪b中的所有更改。
我应该如何实现这样的功能?如果我有相反的情况也很好:
this_session.detach(b) #don't keep track of changes in b
我正在使用postgresql 9.2 和sqlalchemy 0.9.0。
除了一些帮助之外,一个指向文档的链接——处理这个问题的部分——会很棒,所以我可以详细阅读它。 :-)
【问题讨论】:
标签: python postgresql sqlalchemy commit