]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] Improved the "declarative reflection"
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 28 Jan 2012 23:16:46 +0000 (18:16 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 28 Jan 2012 23:16:46 +0000 (18:16 -0500)
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
examples/declarative_reflection/declarative_reflection.py

diff --git a/CHANGES b/CHANGES
index f370a14887db8217da62c5d7b6271b2d2cfbe721..ac787593af270971a408ecb03b994a9513237df6 100644 (file)
--- 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
index 52e6ca65bd35cd26866e5e27cdfabc50f0e82c67..42d2201e1e9f0bd0d022d11a8ac8572f9645ffbe 100644 (file)
@@ -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)