diff options
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/engine/__init__.py | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/engine/base.py | 6 | ||||
-rwxr-xr-x | lib/sqlalchemy/ext/declarative.py | 65 |
3 files changed, 66 insertions, 11 deletions
diff --git a/lib/sqlalchemy/engine/__init__.py b/lib/sqlalchemy/engine/__init__.py index eab462032..0d091bb11 100644 --- a/lib/sqlalchemy/engine/__init__.py +++ b/lib/sqlalchemy/engine/__init__.py @@ -130,6 +130,12 @@ def create_engine(*args, **kwargs): will establish the first actual DBAPI connection when this request is received. The :func:`.create_engine` call itself does **not** establish any actual DBAPI connections directly. + + See also: + + :ref:`engines_toplevel` + + :ref:`connections_toplevel` :param assert_unicode: Deprecated. A warning is raised in all cases when a non-Unicode object is passed when SQLAlchemy would coerce into an encoding diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 04c14c2b7..a5f5ae223 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -2068,6 +2068,12 @@ class Engine(Connectable, log.Identified): An :class:`.Engine` object is instantiated publically using the :func:`~sqlalchemy.create_engine` function. + See also: + + :ref:`engines_toplevel` + + :ref:`connections_toplevel` + """ _execution_options = util.immutabledict() diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 15e7ba34f..434cc9584 100755 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -512,20 +512,32 @@ using the declarative form, via a special base class that defers the creation of the mapper. That recipe is available at `DeclarativeAbstractConcreteBase <http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DeclarativeAbstractConcreteBase>`_ -Mixin Classes -============== +.. _declarative_mixins: -A common need when using :mod:`~sqlalchemy.ext.declarative` is to -share some functionality, often a set of columns, across many -classes. The normal Python idiom would be to put this common code into -a base class and have all the other classes subclass this class. +Mixin and Custom Base Classes +============================== -When using :mod:`~sqlalchemy.ext.declarative`, this need is met by -using a "mixin class". A mixin class is one that isn't mapped to a -table and doesn't subclass the declarative :class:`.Base`. For example:: +A common need when using :mod:`~sqlalchemy.ext.declarative` is to +share some functionality, such as a set of common columns, some common +table options, or other mapped properties, across many +classes. The standard Python idioms for this is to have the classes +inherit from a base which includes these common features. + +When using :mod:`~sqlalchemy.ext.declarative`, this idiom is allowed +via the usage of a custom declarative base class, as well as a "mixin" class +which is inherited from in addition to the primary base. Declarative +includes several helper features to make this work in terms of how +mappings are declared. An example of some commonly mixed-in +idioms is below:: + from sqlalchemy.ext.declarative import declared_attr + class MyMixin(object): + @declared_attr + def __tablename__(cls): + return cls.__name__.lower() + __table_args__ = {'mysql_engine': 'InnoDB'} __mapper_args__= {'always_refresh': True} @@ -538,8 +550,39 @@ table and doesn't subclass the declarative :class:`.Base`. For example:: name = Column(String(1000)) Where above, the class ``MyModel`` will contain an "id" column -as well as ``__table_args__`` and ``__mapper_args__`` defined -by the ``MyMixin`` mixin class. +as the primary key, a ``__tablename__`` attribute that derives +from the name of the class itself, as well as ``__table_args__`` +and ``__mapper_args__`` defined by the ``MyMixin`` mixin class. + +Augmenting the Base +~~~~~~~~~~~~~~~~~~~ + +In addition to using a pure mixin, most of the techniques in this +section can also be applied to the base class itself, for patterns that +should apply to all classes derived from a particular base. This +is achieved using the ``cls`` argument of the :func:`.declarative_base` function:: + + from sqlalchemy.ext.declarative import declared_attr + + class Base(object): + @declared_attr + def __tablename__(cls): + return cls.__name__.lower() + + __table_args__ = {'mysql_engine': 'InnoDB'} + + id = Column(Integer, primary_key=True) + + from sqlalchemy.ext.declarative import declarative_base + + Base = declarative_base(cls=Base) + + class MyModel(Base): + name = Column(String(1000)) + +Where above, ``MyModel`` and all other classes that derive from ``Base`` will have +a table name derived from the class name, an ``id`` primary key column, as well as +the "InnoDB" engine for MySQL. Mixing in Columns ~~~~~~~~~~~~~~~~~ |