]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- make a new page that introduces mapping a little better for the
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 Jan 2015 19:20:03 +0000 (14:20 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 Jan 2015 19:21:11 +0000 (14:21 -0500)
"mapping" section, contrasts declarative and classical some more

Conflicts:
doc/build/orm/mapper_config.rst

doc/build/orm/classical.rst
doc/build/orm/mapper_config.rst
doc/build/orm/mapping_styles.rst [new file with mode: 0644]
doc/build/orm/relationships.rst

index 0f04586c73e1c7a5e64fdf13231d3612d23b984c..3fd149f92853f7c19d13be7a5882c33dd8c458b6 100644 (file)
@@ -1,68 +1,5 @@
-.. _classical_mapping:
+:orphan:
 
-Classical Mappings
-==================
+Moved!   :ref:`classical_mapping`
 
-A *Classical Mapping* refers to the configuration of a mapped class using the
-:func:`.mapper` function, without using the Declarative system.   As an example,
-start with the declarative mapping introduced in :ref:`ormtutorial_toplevel`::
 
-    class User(Base):
-        __tablename__ = 'users'
-
-        id = Column(Integer, primary_key=True)
-        name = Column(String)
-        fullname = Column(String)
-        password = Column(String)
-
-In "classical" form, the table metadata is created separately with the :class:`.Table`
-construct, then associated with the ``User`` class via the :func:`.mapper` function::
-
-    from sqlalchemy import Table, MetaData, Column, ForeignKey, Integer, String
-    from sqlalchemy.orm import mapper
-
-    metadata = MetaData()
-
-    user = Table('user', metadata,
-                Column('id', Integer, primary_key=True),
-                Column('name', String(50)),
-                Column('fullname', String(50)),
-                Column('password', String(12))
-            )
-
-    class User(object):
-        def __init__(self, name, fullname, password):
-            self.name = name
-            self.fullname = fullname
-            self.password = password
-
-    mapper(User, user)
-
-Information about mapped attributes, such as relationships to other classes, are provided
-via the ``properties`` dictionary.  The example below illustrates a second :class:`.Table`
-object, mapped to a class called ``Address``, then linked to ``User`` via :func:`.relationship`::
-
-    address = Table('address', metadata,
-                Column('id', Integer, primary_key=True),
-                Column('user_id', Integer, ForeignKey('user.id')),
-                Column('email_address', String(50))
-                )
-
-    mapper(User, user, properties={
-        'addresses' : relationship(Address, backref='user', order_by=address.c.id)
-    })
-
-    mapper(Address, address)
-
-When using classical mappings, classes must be provided directly without the benefit
-of the "string lookup" system provided by Declarative.  SQL expressions are typically
-specified in terms of the :class:`.Table` objects, i.e. ``address.c.id`` above
-for the ``Address`` relationship, and not ``Address.id``, as ``Address`` may not
-yet be linked to table metadata, nor can we specify a string here.
-
-Some examples in the documentation still use the classical approach, but note that
-the classical as well as Declarative approaches are **fully interchangeable**.  Both
-systems ultimately create the same configuration, consisting of a :class:`.Table`,
-user-defined class, linked together with a :func:`.mapper`.  When we talk about
-"the behavior of :func:`.mapper`", this includes when using the Declarative system
-as well - it's still used, just behind the scenes.
index b986d4cab60d89aea4c3c3de3403ad1dd0bcdd5b..60ad7f5f9a3ed8feda88f16673e4a098399d08d1 100644 (file)
@@ -13,7 +13,7 @@ know how to construct and use rudimentary mappers and relationships.
 .. toctree::
     :maxdepth: 2
 
-    classical
+    mapping_styles
     scalar_mapping
     inheritance
     nonstandard_mappings
diff --git a/doc/build/orm/mapping_styles.rst b/doc/build/orm/mapping_styles.rst
new file mode 100644 (file)
index 0000000..e6be00e
--- /dev/null
@@ -0,0 +1,121 @@
+=================
+Types of Mappings
+=================
+
+Modern SQLAlchemy features two distinct styles of mapper configuration.
+The "Classical" style is SQLAlchemy's original mapping API, whereas
+"Declarative" is the richer and more succinct system that builds on top
+of "Classical".   Both styles may be used interchangeably, as the end
+result of each is exactly the same - a user-defined class mapped by the
+:func:`.mapper` function onto a selectable unit, typically a :class:`.Table`.
+
+Declarative Mapping
+===================
+
+The *Declarative Mapping* is the typical way that
+mappings are constructed in modern SQLAlchemy.
+Making use of the :ref:`declarative_toplevel`
+system, the components of the user-defined class as well as the
+:class:`.Table` metadata to which the class is mapped are defined
+at once::
+
+    from sqlalchemy.ext.declarative import declarative_base
+    from sqlalchemy import Column, Integer, String, ForeignKey
+
+    Base = declarative_base()
+
+    class User(Base):
+        __tablename__ = 'user'
+
+        id = Column(Integer, primary_key=True)
+        name = Column(String)
+        fullname = Column(String)
+        password = Column(String)
+
+Above, a basic single-table mapping with four columns.   Additional
+attributes, such as relationships to other mapped classes, are also
+declared inline within the class definition::
+
+    class User(Base):
+        __tablename__ = 'user'
+
+        id = Column(Integer, primary_key=True)
+        name = Column(String)
+        fullname = Column(String)
+        password = Column(String)
+
+        addresses = relationship("Address", backref="user", order_by="Address.id")
+
+    class Address(Base):
+        __tablename__ = 'address'
+
+        id = Column(Integer, primary_key=True)
+        user_id = Column(ForeignKey('user.id'))
+        email_address = Column(String)
+
+The declarative mapping system is introduced in the
+:ref:`ormtutorial_toplevel`.  For additional details on how this system
+works, see :ref:`declarative_toplevel`.
+
+.. _classical_mapping:
+
+Classical Mappings
+==================
+
+A *Classical Mapping* refers to the configuration of a mapped class using the
+:func:`.mapper` function, without using the Declarative system.  This is
+SQLAlchemy's original class mapping API, and is still the base mapping
+system provided by the ORM.
+
+In "classical" form, the table metadata is created separately with the
+:class:`.Table` construct, then associated with the ``User`` class via
+the :func:`.mapper` function::
+
+    from sqlalchemy import Table, MetaData, Column, Integer, String, ForeignKey
+    from sqlalchemy.orm import mapper
+
+    metadata = MetaData()
+
+    user = Table('user', metadata,
+                Column('id', Integer, primary_key=True),
+                Column('name', String(50)),
+                Column('fullname', String(50)),
+                Column('password', String(12))
+            )
+
+    class User(object):
+        def __init__(self, name, fullname, password):
+            self.name = name
+            self.fullname = fullname
+            self.password = password
+
+    mapper(User, user)
+
+Information about mapped attributes, such as relationships to other classes, are provided
+via the ``properties`` dictionary.  The example below illustrates a second :class:`.Table`
+object, mapped to a class called ``Address``, then linked to ``User`` via :func:`.relationship`::
+
+    address = Table('address', metadata,
+                Column('id', Integer, primary_key=True),
+                Column('user_id', Integer, ForeignKey('user.id')),
+                Column('email_address', String(50))
+                )
+
+    mapper(User, user, properties={
+        'addresses' : relationship(Address, backref='user', order_by=address.c.id)
+    })
+
+    mapper(Address, address)
+
+When using classical mappings, classes must be provided directly without the benefit
+of the "string lookup" system provided by Declarative.  SQL expressions are typically
+specified in terms of the :class:`.Table` objects, i.e. ``address.c.id`` above
+for the ``Address`` relationship, and not ``Address.id``, as ``Address`` may not
+yet be linked to table metadata, nor can we specify a string here.
+
+Some examples in the documentation still use the classical approach, but note that
+the classical as well as Declarative approaches are **fully interchangeable**.  Both
+systems ultimately create the same configuration, consisting of a :class:`.Table`,
+user-defined class, linked together with a :func:`.mapper`.  When we talk about
+"the behavior of :func:`.mapper`", this includes when using the Declarative system
+as well - it's still used, just behind the scenes.
index 6fea107a7aab5f07dda47d85d713f41c6e31afd5..f5cbac87eba4342eb3ea61a6fdd1f53970295ef8 100644 (file)
@@ -6,9 +6,8 @@ Relationship Configuration
 ==========================
 
 This section describes the :func:`relationship` function and in depth discussion
-of its usage.   The reference material here continues into the next section,
-:ref:`collections_toplevel`, which has additional detail on configuration
-of collections via :func:`relationship`.
+of its usage.   For an introduction to relationships, start with the
+:ref:`ormtutorial_toplevel` and head into :ref:`orm_tutorial_relationship`.
 
 .. toctree::
     :maxdepth: 2