2011-11-04 60 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屬性。

看到自從你問了一段時間,它可能不會爲你服務,但也許給其他人