我不确定使用sqlalchemy.orm.synonym是否可以实现所需的功能。可能没有给出这样的事实,即sqlalchemy如何跟踪哪些实例是脏的并且需要在刷新期间进行更新。
但是还有其他方法可以获取此功能-SessionExtensions(请注意,顶部需要填充的engine_string变量):
(env)zifot@localhost:~/stackoverflow$ cat stackoverflow.pyengine_string = ''from sqlalchemy import Table, Column, String, Integer, metaData, create_engineimport sqlalchemy.orm as ormimport datetimeengine = create_engine(engine_string, echo = True)meta = metaData(bind = engine)foo_table = Table('foo', meta, Column('id', String(3), primary_key=True), Column('description', String(64), nullable=False), Column('calculated_value', Integer, nullable=False),)meta.drop_all()meta.create_all()class MyExt(orm.interfaces.SessionExtension): def before_commit(self, session): for obj in session: if isinstance(obj, Foo): obj.calculated_value = datetime.datetime.now().second + 10Session = orm.sessionmaker(extension = MyExt())()Session.configure(bind = engine)class Foo(object): def __init__(self, id, description): self.id = id self.description = descriptionorm.mapper(Foo, foo_table)(env)zifot@localhost:~/stackoverflow$ ipythonPython 2.5.2 (r252:60911, Jan 4 2009, 17:40:26)Type "copyright", "credits" or "license" for more information.IPython 0.10 -- An enhanced Interactive Python.? -> Introduction and overview of IPython's features.%quickref -> Quick reference.help -> Python's own help system.object? -> Details about 'object'. ?object also works, ?? prints more.In [1]: from stackoverflow import *2010-06-11 13:19:30,925 INFO sqlalchemy.engine.base.Engine.0x...11cc select version()2010-06-11 13:19:30,927 INFO sqlalchemy.engine.base.Engine.0x...11cc {}2010-06-11 13:19:30,935 INFO sqlalchemy.engine.base.Engine.0x...11cc select current_schema()2010-06-11 13:19:30,936 INFO sqlalchemy.engine.base.Engine.0x...11cc {}2010-06-11 13:19:30,965 INFO sqlalchemy.engine.base.Engine.0x...11cc select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_schema() and lower(relname)=%(name)s2010-06-11 13:19:30,966 INFO sqlalchemy.engine.base.Engine.0x...11cc {'name': u'foo'}2010-06-11 13:19:30,979 INFO sqlalchemy.engine.base.Engine.0x...11ccDROp TABLE foo2010-06-11 13:19:30,980 INFO sqlalchemy.engine.base.Engine.0x...11cc {}2010-06-11 13:19:30,988 INFO sqlalchemy.engine.base.Engine.0x...11cc COMMIT2010-06-11 13:19:30,997 INFO sqlalchemy.engine.base.Engine.0x...11cc select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_schema() and lower(relname)=%(name)s2010-06-11 13:19:30,999 INFO sqlalchemy.engine.base.Engine.0x...11cc {'name': u'foo'}2010-06-11 13:19:31,007 INFO sqlalchemy.engine.base.Engine.0x...11ccCREATE TABLE foo ( id VARCHAr(3) NOT NULL, description VARCHAr(64) NOT NULL, calculated_value INTEGER NOT NULL, PRIMARY KEY (id))2010-06-11 13:19:31,009 INFO sqlalchemy.engine.base.Engine.0x...11cc {}2010-06-11 13:19:31,025 INFO sqlalchemy.engine.base.Engine.0x...11cc COMMITIn [2]: f = Foo('idx', 'foo')In [3]: f.calculated_valueIn [4]: Session.add(f)In [5]: f.calculated_valueIn [6]: Session.commit()2010-06-11 13:19:57,668 INFO sqlalchemy.engine.base.Engine.0x...11cc BEGIN2010-06-11 13:19:57,674 INFO sqlalchemy.engine.base.Engine.0x...11cc INSERT INTO foo (id, description, calculated_value) VALUES (%(id)s, %(description)s, %(calculated_value)s)2010-06-11 13:19:57,675 INFO sqlalchemy.engine.base.Engine.0x...11cc {'description': 'foo', 'calculated_value': 67, 'id': 'idx'}2010-06-11 13:19:57,683 INFO sqlalchemy.engine.base.Engine.0x...11cc COMMITIn [7]: f.calculated_value2010-06-11 13:20:00,755 INFO sqlalchemy.engine.base.Engine.0x...11cc BEGIN2010-06-11 13:20:00,759 INFO sqlalchemy.engine.base.Engine.0x...11cc SELECt foo.id AS foo_id, foo.description AS foo_description, foo.calculated_value AS foo_calculated_valueFROM fooWHERe foo.id = %(param_1)s2010-06-11 13:20:00,761 INFO sqlalchemy.engine.base.Engine.0x...11cc {'param_1': 'idx'}Out[7]: 67In [8]: f.calculated_valueOut[8]: 67In [9]: Session.commit()2010-06-11 13:20:08,366 INFO sqlalchemy.engine.base.Engine.0x...11cc UPDATe foo SET calculated_value=%(calculated_value)s WHERe foo.id = %(foo_id)s2010-06-11 13:20:08,367 INFO sqlalchemy.engine.base.Engine.0x...11cc {'foo_id': u'idx', 'calculated_value': 18}2010-06-11 13:20:08,373 INFO sqlalchemy.engine.base.Engine.0x...11cc COMMITIn [10]: f.calculated_value2010-06-11 13:20:10,475 INFO sqlalchemy.engine.base.Engine.0x...11cc BEGIN2010-06-11 13:20:10,479 INFO sqlalchemy.engine.base.Engine.0x...11cc SELECT foo.id AS foo_id, foo.description AS foo_description, foo.calculated_value AS foo_calculated_valueFROM fooWHERe foo.id = %(param_1)s2010-06-11 13:20:10,481 INFO sqlalchemy.engine.base.Engine.0x...11cc {'param_1': 'idx'}Out[10]: 18
有关SessionExtensions的更多信息:sqlalchemy.orm.interfaces.SessionExtension。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)