[Sqlalchemy-commits] sqlalchemy: - [bug] Improved the "declarative reflection"
Brought to you by:
zzzeek
From: <co...@sq...> - 2012-01-28 23:17:03
|
details: http://hg.sqlalchemy.org/sqlalchemy/sqlalchemy/rev/56bc623cffab changeset: 8074:56bc623cffab user: Mike Bayer <mi...@zz...> date: Sat Jan 28 18:16:46 2012 -0500 description: - [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: CHANGES | 10 +++ examples/declarative_reflection/declarative_reflection.py | 41 +++++++++----- 2 files changed, 36 insertions(+), 15 deletions(-) diffs (85 lines): diff -r 587800a16a45 -r 56bc623cffab CHANGES --- a/CHANGES Sat Jan 28 17:47:53 2012 -0500 +++ b/CHANGES Sat Jan 28 18:16:46 2012 -0500 @@ -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 -r 587800a16a45 -r 56bc623cffab examples/declarative_reflection/declarative_reflection.py --- a/examples/declarative_reflection/declarative_reflection.py Sat Jan 28 17:47:53 2012 -0500 +++ b/examples/declarative_reflection/declarative_reflection.py Sat Jan 28 18:16:46 2012 -0500 @@ -19,27 +19,38 @@ @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() - class Foo(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(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 @@ ) """) - Base.prepare(e) + Reflected.prepare(e) s = Session(e) |