From 8e8e6a88b7259e1b10f573a6f826c928647542c7 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 28 Jan 2012 18:16:46 -0500 Subject: [PATCH] - [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. --- CHANGES | 10 +++++ .../declarative_reflection.py | 41 ++++++++++++------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/CHANGES b/CHANGES index f370a14887..ac787593af 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,16 @@ ======= CHANGES ======= +0.7.6 +===== +- orm + - [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. + 0.7.5 (January 28, 2012) ===== - orm diff --git a/examples/declarative_reflection/declarative_reflection.py b/examples/declarative_reflection/declarative_reflection.py index 52e6ca65bd..42d2201e1e 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) -- 2.47.2