diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-01-28 18:16:46 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-01-28 18:16:46 -0500 |
| commit | 8e8e6a88b7259e1b10f573a6f826c928647542c7 (patch) | |
| tree | d2672378c3fdd707252889b19f0995a3e32d14c2 /examples | |
| parent | 9ce9039c3fc9fe7f56b3e9d6acc2308568624761 (diff) | |
| download | sqlalchemy-8e8e6a88b7259e1b10f573a6f826c928647542c7.tar.gz | |
- [bug] Improved the "declarative reflection"
example to support single-table inheritance,
multiple calls to prepare(), tables that
are present in alternate schemas,
establishing only a subset of classes
as reflected.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/declarative_reflection/declarative_reflection.py | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/examples/declarative_reflection/declarative_reflection.py b/examples/declarative_reflection/declarative_reflection.py index 52e6ca65b..42d2201e1 100644 --- a/examples/declarative_reflection/declarative_reflection.py +++ b/examples/declarative_reflection/declarative_reflection.py @@ -19,27 +19,38 @@ class DeclarativeReflectedBase(object): @classmethod def prepare(cls, engine): """Reflect all the tables and map !""" - for args, kw in cls._mapper_args: + while cls._mapper_args: + args, kw = cls._mapper_args.pop() klass = args[0] - klass.__table__ = table = Table( - klass.__tablename__, - cls.metadata, - extend_existing=True, - autoload_replace=False, - autoload=True, - autoload_with=engine, - ) - klass.__mapper__ = mapper(klass, table, **kw) - + # autoload Table, which is already + # present in the metadata. This + # will fill in db-loaded columns + # into the existing Table object. + if args[1] is not None: + table = args[1] + Table(table.name, + cls.metadata, + extend_existing=True, + autoload_replace=False, + autoload=True, + autoload_with=engine, + schema=table.schema) + klass.__mapper__ = mapper(*args, **kw) if __name__ == '__main__': - Base= declarative_base(cls=DeclarativeReflectedBase) + Base = declarative_base() + + # create a separate base so that we can + # define a subset of classes as "Reflected", + # instead of everything. + class Reflected(DeclarativeReflectedBase, Base): + __abstract__ = True - class Foo(Base): + class Foo(Reflected): __tablename__ = 'foo' bars = relationship("Bar") - class Bar(Base): + class Bar(Reflected): __tablename__ = 'bar' # illustrate overriding of "bar.foo_id" to have @@ -63,7 +74,7 @@ if __name__ == '__main__': ) """) - Base.prepare(e) + Reflected.prepare(e) s = Session(e) |
