I would like to use autoload to use an existing database. I know how to do this without declarative syntax (model / _init_.py):
def init_model(engine): """Call me before using any of the tables or classes in the model""" t_events = Table('events', Base.metadata, schema='events', autoload=True, autoload_with=engine) orm.mapper(Event, t_events) Session.configure(bind=engine) class Event(object): pass
This works fine, but I would like to use declarative syntax:
class Event(Base): __tablename__ = 'events' __table_args__ = {'schema': 'events', 'autoload': True}
Unfortunately, this way I get:
sqlalchemy.exc.UnboundExecutionError: no engines are associated with this MetaData table. Pass the engine to the table through autoload_with = <someengine>, or associate MetaData with the engine through metadata.bind = <someengine>
The problem is that I do not know where to get the engine (use it in autoload_with) at the stage of importing the model (it is available in init_model ()). I tried adding
meta.Base.metadata.bind(engine)
for environment.py, but it does not work. Has anyone found some elegant solution?
python reflection declarative sqlalchemy pylons
Juliusz Gonera
source share