|
@@ -17,7 +17,7 @@ A Brief History
|
|
|
|
|
|
Prior to version 1.7, Django only supported adding new models to the
|
|
|
database; it was not possible to alter or remove existing models via the
|
|
|
-``syncdb`` command (the predecessor to ``migrate``).
|
|
|
+``syncdb`` command (the predecessor to :djadmin:`migrate`).
|
|
|
|
|
|
Third-party tools, most notably `South <http://south.aeracode.org>`_,
|
|
|
provided support for these additional types of change, but it was considered
|
|
@@ -53,7 +53,8 @@ staging machines, and eventually your production machines.
|
|
|
|
|
|
.. note::
|
|
|
It is possible to override the name of the package which contains the
|
|
|
- migrations on a per-app basis by modifying the :setting:`MIGRATION_MODULES` setting.
|
|
|
+ migrations on a per-app basis by modifying the :setting:`MIGRATION_MODULES`
|
|
|
+ setting.
|
|
|
|
|
|
Migrations will run the same way on the same dataset and produce consistent
|
|
|
results, meaning that what you see in development and staging is, under the
|
|
@@ -184,14 +185,14 @@ Dependencies
|
|
|
While migrations are per-app, the tables and relationships implied by
|
|
|
your models are too complex to be created for just one app at a time. When
|
|
|
you make a migration that requires something else to run - for example,
|
|
|
-you add a ForeignKey in your ``books`` app to your ``authors`` app - the
|
|
|
+you add a ``ForeignKey`` in your ``books`` app to your ``authors`` app - the
|
|
|
resulting migration will contain a dependency on a migration in ``authors``.
|
|
|
|
|
|
This means that when you run the migrations, the ``authors`` migration runs
|
|
|
first and creates the table the ``ForeignKey`` references, and then the migration
|
|
|
that makes the ``ForeignKey`` column runs afterwards and creates the constraint.
|
|
|
-If this didn't happen, the migration would try to create the ForeignKey column
|
|
|
-without the table it's referencing existing and your database would
|
|
|
+If this didn't happen, the migration would try to create the ``ForeignKey``
|
|
|
+column without the table it's referencing existing and your database would
|
|
|
throw an error.
|
|
|
|
|
|
This dependency behavior affects most migration operations where you
|
|
@@ -228,8 +229,8 @@ inspects this object for four attributes, only two of which are used
|
|
|
most of the time:
|
|
|
|
|
|
* ``dependencies``, a list of migrations this one depends on.
|
|
|
-* ``operations``, a list of Operation classes that define what this migration
|
|
|
- does.
|
|
|
+* ``operations``, a list of ``Operation`` classes that define what this
|
|
|
+ migration does.
|
|
|
|
|
|
The operations are the key; they are a set of declarative instructions which
|
|
|
tell Django what schema changes need to be made. Django scans them and
|
|
@@ -252,9 +253,9 @@ Custom fields
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
You can't modify the number of positional arguments in an already migrated
|
|
|
-custom field without raising a TypeError. The old migration will call the
|
|
|
+custom field without raising a ``TypeError``. The old migration will call the
|
|
|
modified ``__init__`` method with the old signature. So if you need a new
|
|
|
-argument, please create a keyword argument and use e.g.
|
|
|
+argument, please create a keyword argument and add something like
|
|
|
``assert kwargs.get('argument_name') is not None`` in the constructor.
|
|
|
|
|
|
Adding migrations to apps
|
|
@@ -285,7 +286,6 @@ Note that this only works given two things:
|
|
|
that your database doesn't match your models, you'll just get errors when
|
|
|
migrations try to modify those tables.
|
|
|
|
|
|
-
|
|
|
.. _historical-models:
|
|
|
|
|
|
Historical models
|
|
@@ -293,9 +293,9 @@ Historical models
|
|
|
|
|
|
When you run migrations, Django is working from historical versions of
|
|
|
your models stored in the migration files. If you write Python code
|
|
|
-using the ``django.db.migrations.RunPython`` operation, or if you have
|
|
|
-``allow_migrate`` methods on your database routers, you will be exposed
|
|
|
-to these versions of your models.
|
|
|
+using the :class:`~django.db.migrations.operations.RunPython` operation, or if
|
|
|
+you have ``allow_migrate`` methods on your database routers, you will be
|
|
|
+exposed to these versions of your models.
|
|
|
|
|
|
Because it's impossible to serialize arbitrary Python code, these historical
|
|
|
models will not have any custom methods or managers that you have defined.
|
|
@@ -304,9 +304,9 @@ They will, however, have the same fields, relationships and ``Meta`` options
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
- This means that you will NOT have custom save() methods called on objects
|
|
|
- when you access them in migrations, and you will NOT have any custom constructors
|
|
|
- or instance methods. Plan appropriately!
|
|
|
+ This means that you will NOT have custom ``save()`` methods called on objects
|
|
|
+ when you access them in migrations, and you will NOT have any custom
|
|
|
+ constructors or instance methods. Plan appropriately!
|
|
|
|
|
|
In addition, the base classes of the model are just stored as pointers,
|
|
|
so you must always keep base classes around for as long as there is a migration
|
|
@@ -314,7 +314,6 @@ that contains a reference to them. On the plus side, methods and managers
|
|
|
from these base classes inherit normally, so if you absolutely need access
|
|
|
to these you can opt to move them into a superclass.
|
|
|
|
|
|
-
|
|
|
.. _data-migrations:
|
|
|
|
|
|
Data Migrations
|
|
@@ -330,7 +329,7 @@ Django can't automatically generate data migrations for you, as it does with
|
|
|
schema migrations, but it's not very hard to write them. Migration files in
|
|
|
Django are made up of :doc:`Operations </ref/migration-operations>`, and
|
|
|
the main operation you use for data migrations is
|
|
|
-:ref:`RunPython <operation-run-python>`.
|
|
|
+:class:`~django.db.migrations.operations.RunPython`.
|
|
|
|
|
|
To start, make an empty migration file you can work from (Django will put
|
|
|
the file in the right place, suggest a name, and add dependencies for you)::
|
|
@@ -351,13 +350,15 @@ Then, open up the file; it should look something like this::
|
|
|
operations = [
|
|
|
]
|
|
|
|
|
|
-Now, all you need to do is create a new function and have RunPython use it.
|
|
|
-RunPython expects a callable as its argument which takes two arguments - the
|
|
|
-first is an :doc:`app registry </ref/applications/>` that has the historical
|
|
|
-versions of all your models loaded into it to match where in your history the
|
|
|
-migration sits, and the second is a :doc:`SchemaEditor </ref/schema-editor>`,
|
|
|
-which you can use to manually effect database schema changes (but beware,
|
|
|
-doing this can confuse the migration autodetector!)
|
|
|
+Now, all you need to do is create a new function and have
|
|
|
+:class:`~django.db.migrations.operations.RunPython` use it.
|
|
|
+:class:`~django.db.migrations.operations.RunPython` expects a callable as its argument
|
|
|
+which takes two arguments - the first is an :doc:`app registry
|
|
|
+</ref/applications/>` that has the historical versions of all your models
|
|
|
+loaded into it to match where in your history the migration sits, and the
|
|
|
+second is a :doc:`SchemaEditor </ref/schema-editor>`, which you can use to
|
|
|
+manually effect database schema changes (but beware, doing this can confuse
|
|
|
+the migration autodetector!)
|
|
|
|
|
|
Let's write a simple migration that populates our new ``name`` field with the
|
|
|
combined values of ``first_name`` and ``last_name`` (we've come to our senses
|
|
@@ -389,8 +390,8 @@ Once that's done, we can just run ``python manage.py migrate`` as normal and
|
|
|
the data migration will run in place alongside other migrations.
|
|
|
|
|
|
If you're interested in the more advanced migration operations, or want
|
|
|
-to be able to write your own, see our
|
|
|
-:doc:`migration operations reference </ref/migration-operations>`.
|
|
|
+to be able to write your own, see the :doc:`migration operations reference
|
|
|
+</ref/migration-operations>`.
|
|
|
|
|
|
.. _migration-squashing:
|
|
|
|
|
@@ -406,15 +407,19 @@ Squashing is the act of reducing an existing set of many migrations down to
|
|
|
one (or sometimes a few) migrations which still represent the same changes.
|
|
|
|
|
|
Django does this by taking all of your existing migrations, extracting their
|
|
|
-Operations and putting them all in sequence, and then running an optimizer
|
|
|
+``Operation``\s and putting them all in sequence, and then running an optimizer
|
|
|
over them to try and reduce the length of the list - for example, it knows
|
|
|
-that ``CreateModel`` and ``DeleteModel`` cancel each other out, and it knows
|
|
|
-that ``AddColumn`` can be rolled into ``CreateModel``.
|
|
|
+that :class:`~django.db.migrations.operations.CreateModel` and
|
|
|
+:class:`~django.db.migrations.operations.DeleteModel` cancel each other out,
|
|
|
+and it knows that :class:`~django.db.migrations.operations.AddField` can be
|
|
|
+rolled into :class:`~django.db.migrations.operations.CreateModel`.
|
|
|
|
|
|
Once the operation sequence has been reduced as much as possible - the amount
|
|
|
possible depends on how closely intertwined your models are and if you have
|
|
|
-any RunSQL or RunPython operations (which can't be optimized through) - Django
|
|
|
-will them write it back out into a new set of initial migration files.
|
|
|
+any :class:`~django.db.migrations.operations.RunSQL`
|
|
|
+or :class:`~django.db.migrations.operations.RunPython` operations (which can't
|
|
|
+be optimized through) - Django will them write it back out into a new set of
|
|
|
+initial migration files.
|
|
|
|
|
|
These files are marked to say they replace the previously-squashed migrations,
|
|
|
so they can coexist with the old migration files, and Django will intelligently
|
|
@@ -452,9 +457,9 @@ work::
|
|
|
Note that model interdependencies in Django can get very complex, and squashing
|
|
|
may occasionally result in an optimized migration that doesn't work or is
|
|
|
impossible to run. When this occurs, you can re-try with ``--no-optimize``, but
|
|
|
-please file a bug report either way detailing the models and their
|
|
|
-relationships so we can improve the optimizer to handle your case.
|
|
|
-
|
|
|
+please `file a bug report <https://code.djangoproject.com/newticket>`_ either
|
|
|
+way detailing the models and their relationships so we can improve the
|
|
|
+optimizer to handle your case.
|
|
|
|
|
|
.. _migration-serializing:
|
|
|
|
|
@@ -508,7 +513,6 @@ available at the top level of a module it is not serializable.
|
|
|
Django will write out the value as an instantiation of your class with the
|
|
|
given arguments, similar to the way it writes out references to Django fields.
|
|
|
|
|
|
-
|
|
|
Upgrading from South
|
|
|
--------------------
|
|
|
|
|
@@ -517,9 +521,13 @@ If you already have pre-existing migrations created with
|
|
|
``django.db.migrations`` is quite simple:
|
|
|
|
|
|
* Ensure all installs are fully up-to-date with their migrations
|
|
|
-* Delete all your (numbered) migration files, but not the directory or __init__.py - make sure you remove the ``.pyc`` files too.
|
|
|
-* Run ``python manage.py makemigrations``. Django should see the empty migration directories and make new initial migrations in the new format.
|
|
|
-* Run ``python manage.py migrate``. Django will see that the tables for the initial migrations already exist and mark them as applied without running them.
|
|
|
+* Delete all your (numbered) migration files, but not the directory or
|
|
|
+ ``__init__.py`` - make sure you remove the ``.pyc`` files too.
|
|
|
+* Run ``python manage.py makemigrations``. Django should see the empty
|
|
|
+ migration directories and make new initial migrations in the new format.
|
|
|
+* Run ``python manage.py migrate``. Django will see that the tables for the
|
|
|
+ initial migrations already exist and mark them as applied without running
|
|
|
+ them.
|
|
|
|
|
|
That's it! The only complication is if you have a circular dependency loop
|
|
|
of foreign keys; in this case, ``makemigrations`` might make more than one
|