1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

[1.8.x] Fixed #24184 -- Prevented automatic soft-apply of migrations

Previously Django only checked for the table name in CreateModel
operations in initial migrations and faked the migration automatically.
This led to various errors and unexpected behavior. The newly introduced
--fake-initial flag to the migrate command must be passed to get the
same behavior again. With this change Django will bail out in with a
"duplicate relation / table" error instead.

Thanks Carl Meyer and Tim Graham for the documentation update, report
and review.

Backport of f287bec583 from master
This commit is contained in:
Markus Holtermann
2015-02-12 12:48:28 +01:00
parent 4ba7d73f94
commit bd80fa6b0f
7 changed files with 130 additions and 23 deletions

View File

@@ -775,6 +775,19 @@ be warned that using ``--fake`` runs the risk of putting the migration state
table into a state where manual recovery will be needed to make migrations
run correctly.
.. versionadded:: 1.8
.. django-admin-option:: --fake-initial
The ``--fake-initial`` option can be used to allow Django to skip an app's
initial migration if all database tables with the names of all models created
by all :class:`~django.db.migrations.operations.CreateModel` operations in that
migration already exist. This option is intended for use when first running
migrations against a database that preexisted the use of migrations. This
option does not, however, check for matching database schema beyond matching
table names and so is only safe to use if you are confident that your existing
schema matches what is recorded in your initial migration.
.. deprecated:: 1.8
The ``--list`` option has been moved to the :djadmin:`showmigrations`

View File

@@ -1134,6 +1134,11 @@ Miscellaneous
has been removed by a migration and replaced by a property. That means it's
not possible to query or filter a ``ContentType`` by this field any longer.
* :djadmin:`migrate` now accepts the :djadminopt:`--fake-initial` option to
allow faking initial migrations. In 1.7 initial migrations were always
automatically faked if all tables created in an initial migration already
existed.
.. _deprecated-features-1.8:
Features deprecated in 1.8

View File

@@ -167,6 +167,13 @@ developers (or your production servers) check out the code, they'll
get both the changes to your models and the accompanying migration at the
same time.
.. versionadded:: 1.8
If you want to give the migration(s) a meaningful name instead of a generated
one, you can use the :djadminopt:`--name` option::
$ python manage.py makemigrations --name changed_my_model your_app_label
Version control
~~~~~~~~~~~~~~~
@@ -329,10 +336,12 @@ need to convert it to use migrations; this is a simple process::
$ python manage.py makemigrations your_app_label
This will make a new initial migration for your app. Now, when you run
:djadmin:`migrate`, Django will detect that you have an initial migration
*and* that the tables it wants to create already exist, and will mark the
migration as already applied.
This will make a new initial migration for your app. Now, run ``python
manage.py migrate --fake-initial``, and Django will detect that you have an
initial migration *and* that the tables it wants to create already exist, and
will mark the migration as already applied. (Without the
:djadminopt:`--fake-initial` flag, the :djadmin:`migrate` command would error
out because the tables it wants to create already exist.)
Note that this only works given two things:
@@ -344,12 +353,11 @@ 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.
.. versionadded:: 1.8
.. versionchanged: 1.8
If you want to give the migration(s) a meaningful name instead of a generated one,
you can use the :djadminopt:`--name` option::
$ python manage.py makemigrations --name changed_my_model your_app_label
The ``--fake-initial`` flag to :djadmin:`migrate` was added. Previously,
Django would always automatically fake-apply initial migrations if it
detected that the tables exist.
.. _historical-models:
@@ -757,9 +765,10 @@ If you already have pre-existing migrations created with
``__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.
* Run ``python manage.py migrate --fake-initial``. Django will see that the
tables for the initial migrations already exist and mark them as applied
without running them. (Django won't check that the table schema match your
models, just that the right table names exist).
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
@@ -767,6 +776,12 @@ initial migration, and you'll need to mark them all as applied using::
python manage.py migrate --fake yourappnamehere
.. versionchanged:: 1.8
The :djadminopt:`--fake-initial` flag was added to :djadmin:`migrate`;
previously, initial migrations were always automatically fake-applied if
existing tables were detected.
Libraries/Third-party Apps
~~~~~~~~~~~~~~~~~~~~~~~~~~