When creating tables via Table() calls, it's obvious how to indicate
which engine should be used. With ActiveMapper, on the other hand,
it's... less obvious. I've tried assuming that a reference to the first
engine I created would be stored in the thread-local storage area if I
have the threadlocal module imported, but that doesn't appear to be the
case. Any hints?
This is what I'm trying:
======================================================================
import sqlalchemy.mods.threadlocal
from sqlalchemy import *
from sqlalchemy.ext.activemapper import *
__engine__ = create_engine('sqlite:///:memory:')
class Foo(ActiveMapper):
class mapping:
__table__ = 'foo_tbl'
foo_id = column(Integer, primary_key=True)
foo_text = column(String(30))
Foo(foo_id=1, foo_text='qux').flush()
======================================================================
Sadly, running it leads to ugliness:
Traceback (most recent call last):
File "test1.py", line 14, in ?
Foo(foo_id=1, foo_text='qux').flush()
File
"/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/ext/assignmapper.py", line
16, in do
return getattr(session, name)(self, *args, **kwargs)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 233, in flush
self.uow.flush(self, objects, echo=self.echo_uow)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py",
line 252, in flush
flush_context.execute(echo=echo)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py",
line 420, in execute
head.execute(self)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py",
line 688, in execute
self._save_objects(trans)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/unitofwork.py",
line 642, in _save_objects
task.mapper.save_obj(task.tosave_objects, trans)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/mapper.py", line
691, in save_obj
connection = uow.transaction.connection(self)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 23, in connection
engine = self.session.get_bind(mapper_or_class)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/orm/session.py",
line 172, in get_bind
e = mapper.mapped_table.engine
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/schema.py", line 41,
in <lambda>
engine = property(lambda s:s._get_engine())
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/schema.py", line 40,
in _get_engine
return self._derived_metadata().engine
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/schema.py", line
709, in <lambda>
engine=property(lambda s:s.context._engine)
File "/home/ccd/VC/svn/sqlalchemy/lib/sqlalchemy/util.py", line 189,
in __getattr__
raise AttributeError(key)
AttributeError: _engine
Thanks!
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users