summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-05-17 11:45:05 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2012-05-17 11:45:05 -0400
commit29251675503271fc700a6f7655157850e2de426d (patch)
tree0a6f5a43525fc2e5390716812e511893cc8e9ce7 /examples
parentda8032dc45ad8243323e9359f9b31efe1b7cfe5b (diff)
downloadsqlalchemy-29251675503271fc700a6f7655157850e2de426d.tar.gz
- [feature] The "deferred declarative
reflection" system has been moved into the declarative extension itself, using the new DeferredReflection class. This class is now tested with both single and joined table inheritance use cases. [ticket:2485] - [bug] The autoload_replace flag on Table, when False, will cause any reflected foreign key constraints which refer to already-declared columns to be skipped, assuming that the in-Python declared column will take over the task of specifying in-Python ForeignKey or ForeignKeyConstraint declarations.
Diffstat (limited to 'examples')
-rw-r--r--examples/declarative_reflection/__init__.py46
-rw-r--r--examples/declarative_reflection/declarative_reflection.py98
2 files changed, 0 insertions, 144 deletions
diff --git a/examples/declarative_reflection/__init__.py b/examples/declarative_reflection/__init__.py
deleted file mode 100644
index cadd6ab24..000000000
--- a/examples/declarative_reflection/__init__.py
+++ /dev/null
@@ -1,46 +0,0 @@
-"""
-Illustrates how to mix table reflection with Declarative, such that
-the reflection process itself can take place **after** all classes
-are defined. Declarative classes can also override column
-definitions loaded from the database.
-
-At the core of this example is the ability to change how Declarative
-assigns mappings to classes. The ``__mapper_cls__`` special attribute
-is overridden to provide a function that gathers mapping requirements
-as they are established, without actually creating the mapping.
-Then, a second class-level method ``prepare()`` is used to iterate
-through all mapping configurations collected, reflect the tables
-named within and generate the actual mappers.
-
-The example is new in 0.7.5 and makes usage of the new
-``autoload_replace`` flag on :class:`.Table` to allow declared
-classes to override reflected columns.
-
-Usage example::
-
- Base = declarative_base(cls=DeclarativeReflectedBase)
-
- class Foo(Base):
- __tablename__ = 'foo'
- bars = relationship("Bar")
-
- class Bar(Base):
- __tablename__ = 'bar'
-
- # illustrate overriding of "bar.foo_id" to have
- # a foreign key constraint otherwise not
- # reflected, such as when using MySQL
- foo_id = Column(Integer, ForeignKey('foo.id'))
-
- Base.prepare(e)
-
- s = Session(e)
-
- s.add_all([
- Foo(bars=[Bar(data='b1'), Bar(data='b2')], data='f1'),
- Foo(bars=[Bar(data='b3'), Bar(data='b4')], data='f2')
- ])
- s.commit()
-
-
-"""
diff --git a/examples/declarative_reflection/declarative_reflection.py b/examples/declarative_reflection/declarative_reflection.py
deleted file mode 100644
index 372149317..000000000
--- a/examples/declarative_reflection/declarative_reflection.py
+++ /dev/null
@@ -1,98 +0,0 @@
-from sqlalchemy import *
-from sqlalchemy.orm import *
-from sqlalchemy.orm.util import _is_mapped_class
-from sqlalchemy.ext.declarative import declarative_base, declared_attr
-
-class DeclarativeReflectedBase(object):
- _mapper_args = []
-
- @classmethod
- def __mapper_cls__(cls, *args, **kw):
- """Declarative will use this function in lieu of
- calling mapper() directly.
-
- Collect each series of arguments and invoke
- them when prepare() is called.
- """
-
- cls._mapper_args.append((args, kw))
-
- @classmethod
- def prepare(cls, engine):
- """Reflect all the tables and map !"""
- while cls._mapper_args:
- args, kw = cls._mapper_args.pop()
- klass = args[0]
- # 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)
-
- # see if we need 'inherits' in the
- # mapper args. Declarative will have
- # skipped this since mappings weren't
- # available yet.
- for c in klass.__bases__:
- if _is_mapped_class(c):
- kw['inherits'] = c
- break
-
- klass.__mapper__ = mapper(*args, **kw)
-
-if __name__ == '__main__':
- 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(Reflected):
- __tablename__ = 'foo'
- bars = relationship("Bar")
-
- class Bar(Reflected):
- __tablename__ = 'bar'
-
- # illustrate overriding of "bar.foo_id" to have
- # a foreign key constraint otherwise not
- # reflected, such as when using MySQL
- foo_id = Column(Integer, ForeignKey('foo.id'))
-
- e = create_engine('sqlite://', echo=True)
- e.execute("""
- create table foo(
- id integer primary key,
- data varchar(30)
- )
- """)
-
- e.execute("""
- create table bar(
- id integer primary key,
- data varchar(30),
- foo_id integer
- )
- """)
-
- Reflected.prepare(e)
-
- s = Session(e)
-
- s.add_all([
- Foo(bars=[Bar(data='b1'), Bar(data='b2')], data='f1'),
- Foo(bars=[Bar(data='b3'), Bar(data='b4')], data='f2')
- ])
- s.commit()
- for f in s.query(Foo):
- print f.data, ",".join([b.data for b in f.bars]) \ No newline at end of file