Commits

Mike Bayer committed 56bc623

- [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.

Comments (0)

Files changed (2)

 =======
 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

examples/declarative_reflection/declarative_reflection.py

     @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 
     )
     """)
 
-    Base.prepare(e)
+    Reflected.prepare(e)
 
     s = Session(e)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.