class DeferredReflection(object):
A helper class for construction of mappings based on a deferred reflection step.
Normally, declarative can be used with reflection by
setting a _schema.Table
object using autoload_with=engine
as the __table__ attribute on a declarative class.
The caveat is that the _schema.Table
must be fully
reflected, or at the very least have a primary key column,
at the point at which a normal declarative mapping is
constructed, meaning the _engine.Engine
must be available
at class declaration time.
The .DeferredReflection
mixin moves the construction
of mappers to be at a later point, after a specific
method is called which first reflects all _schema.Table
objects created so far. Classes can define it as such:
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import DeferredReflection Base = declarative_base() class MyClass(DeferredReflection, Base): __tablename__ = 'mytable'
Above, MyClass is not yet mapped. After a series of
classes have been defined in the above fashion, all tables
can be reflected and mappings created using
.prepare
:
engine = create_engine("someengine://...") DeferredReflection.prepare(engine)
The .DeferredReflection
mixin can be applied to individual
classes, used as the base for the declarative base itself,
or used in a custom abstract class. Using an abstract base
allows that only a subset of classes to be prepared for a
particular prepare step, which is necessary for applications
that use more than one engine. For example, if an application
has two engines, you might use two bases, and prepare each
separately, e.g.:
class ReflectedOne(DeferredReflection, Base): __abstract__ = True class ReflectedTwo(DeferredReflection, Base): __abstract__ = True class MyClass(ReflectedOne): __tablename__ = 'mytable' class MyOtherClass(ReflectedOne): __tablename__ = 'myothertable' class YetAnotherClass(ReflectedTwo): __tablename__ = 'yetanothertable' # ... etc.
Above, the class hierarchies for ReflectedOne and ReflectedTwo can be configured separately:
ReflectedOne.prepare(engine_one) ReflectedTwo.prepare(engine_two)
Class Method | prepare |
Reflect all _schema.Table objects for all current .DeferredReflection subclasses |
Class Method | _reflect_table |
Undocumented |
Class Method | _sa_decl_prepare |
Undocumented |
Class Method | _sa_deferred_table_resolver |
Undocumented |
Class Method | _sa_raise_deferred_config |
Undocumented |