Guest User

Untitled

a guest
Aug 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. SQLAlchemy - Mapping ResultProxy to mapped classes without a session
  2. session.query(SomeClass)
  3.  
  4. engine = create_engine('sqlite:///:memory:')
  5. meta = MetaData()
  6. meta.bind = engine
  7. table = Table('table, meta,
  8. Column('id', Integer, primary_key=True),
  9. Column('field1', String(16), nullable=False),
  10. Column('field2', String(60)),
  11. Column('field3', String(20), nullable=False)
  12. )
  13.  
  14. class Table(object)
  15. pass
  16.  
  17. meta.create_all(checkfirst=True)
  18. for i in range(10):
  19. user.insert({'field1': 'field1'+i,'field2': 'field2'+i*2,'field3': 'field3'+i*4})
  20. mapper(Table, table)
  21. query = Query((Table,))
  22. query.instances(engine.text("SELECT * FROM table").execute())
  23.  
  24. Traceback (most recent call last):
  25. File "sqlalchemy/orm/mapper.py", line 2507, in _instance_processor
  26. session_identity_map = context.session.identity_map
  27. AttributeError: 'NoneType' object has no attribute 'identity_map'
Add Comment
Please, Sign In to add comment