2011-11-04 59 views
2

我会非常想逃避国家通过session查询时结合的实体,并充分利用类映射的不依赖于:SQLAlchemy的 - 映射ResultProxy来映射类没有会话

session.query(SomeClass) 

我没有需要交易,急切/延期加载,更改跟踪或提供的任何其他功能。本质上,我想手动将ResultProxy绑定到映射类,并且有一个没有任何对SQLA的引用(例如状态)的实例列表。

我试过Query.instances,但它需要一个会话实例:

engine = create_engine('sqlite:///:memory:') 
meta = MetaData() 
meta.bind = engine 
table = Table('table, meta, 
    Column('id', Integer, primary_key=True), 
    Column('field1', String(16), nullable=False), 
    Column('field2', String(60)), 
    Column('field3', String(20), nullable=False) 
) 

class Table(object) 
    pass 

meta.create_all(checkfirst=True) 
for i in range(10): 
    user.insert({'field1': 'field1'+i,'field2': 'field2'+i*2,'field3': 'field3'+i*4}) 
mapper(Table, table) 
query = Query((Table,)) 
query.instances(engine.text("SELECT * FROM table").execute()) 

结果:

Traceback (most recent call last): 
    File "sqlalchemy/orm/mapper.py", line 2507, in _instance_processor 
     session_identity_map = context.session.identity_map 
    AttributeError: 'NoneType' object has no attribute 'identity_map' 

我被困在这一点上。我查看了Query.instances,手动设置复制会话似乎非常广泛。它需要Query,QueryContext,_MapperEntity和一个精心编排,这将使大多数芭蕾舞团公司脸红。

TL;博士要使用SQLAlchemy的查询生成(任何返回ResultProxy实例),并有被映射到它们各自的类别而跳过任何与session,identity_map工作&单位绩效。

回答

0

你可以删除对象,所以它不会维护一个会话,但它仍然会有_sa属性。

看到自从你问了一段时间,它可能不会为你服务,但也许给其他人