update sqlalchemy documentation links

use explicit code-block markup
This commit is contained in:
Jan Dittberner 2011-10-29 22:48:52 +02:00
parent 3dc1cf28b0
commit 05e76a1430

View File

@ -7,19 +7,18 @@ Database schema migrations
.. currentmodule:: migrate.changeset.schema
Importing :mod:`migrate.changeset` adds some new methods to existing
SQLAlchemy objects, as well as creating functions of its own. Most operations
can be done either by a method or a function. Methods match
SQLAlchemy's existing API and are more intuitive when the object is
available; functions allow one to make changes when only the name of
an object is available (for example, adding a column to a table in the
database without having to load that table into Python).
Importing :mod:`migrate.changeset` adds some new methods to existing SQLAlchemy
objects, as well as creating functions of its own. Most operations can be done
either by a method or a function. Methods match SQLAlchemy's existing API and
are more intuitive when the object is available; functions allow one to make
changes when only the name of an object is available (for example, adding a
column to a table in the database without having to load that table into
Python).
Changeset operations can be used independently of SQLAlchemy Migrate's
:ref:`versioning <versioning-system>`.
For more information, see the API documentation for
:mod:`migrate.changeset`.
For more information, see the API documentation for :mod:`migrate.changeset`.
.. _summary-changeset-api:
@ -43,17 +42,18 @@ Here are some direct links to the relevent sections of the API documentations:
.. note::
Many of the schema modification methods above take an
``alter_metadata`` keyword parameter. This parameter defaults to
``True``.
Many of the schema modification methods above take an ``alter_metadata``
keyword parameter. This parameter defaults to `True`.
The following sections give examples of how to make various kinds of
schema changes.
The following sections give examples of how to make various kinds of schema
changes.
Column
======
Given a standard SQLAlchemy table::
Given a standard SQLAlchemy table:
.. code-block:: python
table = Table('mytable', meta,
Column('id', Integer, primary_key=True),
@ -62,7 +62,9 @@ Given a standard SQLAlchemy table::
.. _column-create:
You can create a column with :meth:`~ChangesetColumn.create`::
You can create a column with :meth:`~ChangesetColumn.create`:
.. code-block:: python
col = Column('col1', String, default='foobar')
col.create(table, populate_default=True)
@ -76,23 +78,27 @@ You can create a column with :meth:`~ChangesetColumn.create`::
.. note::
You can pass `primary_key_name`, `index_name`
and `unique_name` to the :meth:`~ChangesetColumn.create` method to issue ``ALTER TABLE ADD
You can pass `primary_key_name`, `index_name` and `unique_name` to the
:meth:`~ChangesetColumn.create` method to issue ``ALTER TABLE ADD
CONSTRAINT`` after changing the column.
For multi columns constraints and other advanced configuration,
check the :ref:`constraint tutorial <constraint-tutorial>`.
For multi columns constraints and other advanced configuration, check the
:ref:`constraint tutorial <constraint-tutorial>`.
.. versionadded:: 0.6.0
You can drop a column with :meth:`~ChangesetColumn.drop`::
You can drop a column with :meth:`~ChangesetColumn.drop`:
.. code-block:: python
col.drop()
.. _column-alter:
You can alter a column with :meth:`~ChangesetColumn.alter`::
You can alter a column with :meth:`~ChangesetColumn.alter`:
.. code-block:: python
col.alter(name='col2')
@ -121,11 +127,13 @@ Table
SQLAlchemy includes support for `creating and dropping`__ tables..
Tables can be renamed with :meth:`~ChangesetTable.rename`::
Tables can be renamed with :meth:`~ChangesetTable.rename`:
.. code-block:: python
table.rename('newtablename')
.. __: http://www.sqlalchemy.org/docs/05/metadata.html#creating-and-dropping-database-tables
.. __: http://www.sqlalchemy.org/docs/core/schema.html#creating-and-dropping-database-tables
.. currentmodule:: migrate.changeset.constraint
@ -136,11 +144,14 @@ Index
SQLAlchemy supports `creating and dropping`__ indexes.
Indexes can be renamed using :meth:`~migrate.changeset.schema.ChangesetIndex.rename`::
Indexes can be renamed using
:meth:`~migrate.changeset.schema.ChangesetIndex.rename`:
.. code-block:: python
index.rename('newindexname')
.. __: http://www.sqlalchemy.org/docs/05/metadata.html#indexes
.. __: http://www.sqlalchemy.org/docs/core/schema.html#indexes
.. _constraint-tutorial:
@ -150,27 +161,31 @@ Constraint
.. currentmodule:: migrate.changeset.constraint
SQLAlchemy supports creating or dropping constraints at the same time
a table is created or dropped. SQLAlchemy Migrate adds support for
creating and dropping
:class:`~sqlalchemy.schema.PrimaryKeyConstraint`,
SQLAlchemy supports creating or dropping constraints at the same time a table
is created or dropped. SQLAlchemy Migrate adds support for creating and
dropping :class:`~sqlalchemy.schema.PrimaryKeyConstraint`,
:class:`~sqlalchemy.schema.ForeignKeyConstraint`,
:class:`~sqlalchemy.schema.CheckConstraint` and
:class:`~sqlalchemy.schema.UniqueConstraint` constraints independently
using ``ALTER TABLE`` statements.
:class:`~sqlalchemy.schema.UniqueConstraint` constraints independently using
``ALTER TABLE`` statements.
The following rundowns are true for all constraints classes:
1. Make sure you import the relevent constrain class Migrate and not
from SQLAlchemy, for example::
#. Make sure you import the relevant constraint class from :mod:`migrate` and
not from :mod:`sqlalchemy`, for example:
.. code-block:: python
from migrate.changeset.constraint import ForeignKeyConstraint
The classes in that module have the extra
:meth:`~ConstraintChangeset.create` and
:meth:`~ConstraintChangeset.drop` methods.
:meth:`~ConstraintChangeset.create` and :meth:`~ConstraintChangeset.drop`
methods.
2. You can also use Constraints as in SQLAlchemy. In this case passing table argument explicitly is required::
#. You can also use constraints as in SQLAlchemy. In this case passing table
argument explicitly is required:
.. code-block:: python
cons = PrimaryKeyConstraint('id', 'num', table=self.table)
@ -180,11 +195,16 @@ The following rundowns are true for all constraints classes:
# Drop the constraint
cons.drop()
You can also pass in :class:`~sqlalchemy.schema.Column` objects (and table argument can be left out)::
You can also pass in :class:`~sqlalchemy.schema.Column` objects (and table
argument can be left out):
.. code-block:: python
cons = PrimaryKeyConstraint(col1, col2)
3. Some dialects support ``CASCADE`` option when dropping constraints::
#. Some dialects support ``CASCADE`` option when dropping constraints:
.. code-block:: python
cons = PrimaryKeyConstraint(col1, col2)
@ -194,19 +214,20 @@ The following rundowns are true for all constraints classes:
# Drop the constraint
cons.drop(cascade=True)
.. note::
SQLAlchemy Migrate will try to guess the name of the
constraints for databases, but if it's something other than
the default, you'll need to give its name. Best practice is
to always name your constraints. Note that Oracle requires
that you state the name of the constraint to be created or dropped.
SQLAlchemy Migrate will try to guess the name of the constraints for
databases, but if it's something other than the default, you'll need to
give its name. Best practice is to always name your constraints. Note that
Oracle requires that you state the name of the constraint to be created or
dropped.
Examples
---------
Primary key constraints::
Primary key constraints:
.. code-block:: python
from migrate.changeset.constraint import PrimaryKeyConstraint
@ -218,7 +239,9 @@ Primary key constraints::
# Drop the constraint
cons.drop()
Foreign key constraints::
Foreign key constraints:
.. code-block:: python
from migrate.changeset.constraint import ForeignKeyConstraint
@ -230,7 +253,9 @@ Foreign key constraints::
# Drop the constraint
cons.drop()
Check constraints::
Check constraints:
.. code-block:: python
from migrate.changeset.constraint import CheckConstraint
@ -242,7 +267,9 @@ Check constraints::
# Drop the constraint
cons.drop()
Unique constraints::
Unique constraints:
.. code-block:: python
from migrate.changeset.constraint import UniqueConstraint