1. ==========
    
  2. Migrations
    
  3. ==========
    
  4. 
    
  5. .. module:: django.db.migrations
    
  6.    :synopsis: Schema migration support for Django models
    
  7. 
    
  8. Migrations are Django's way of propagating changes you make to your models
    
  9. (adding a field, deleting a model, etc.) into your database schema. They're
    
  10. designed to be mostly automatic, but you'll need to know when to make
    
  11. migrations, when to run them, and the common problems you might run into.
    
  12. 
    
  13. The Commands
    
  14. ============
    
  15. 
    
  16. There are several commands which you will use to interact with migrations
    
  17. and Django's handling of database schema:
    
  18. 
    
  19. * :djadmin:`migrate`, which is responsible for applying and unapplying
    
  20.   migrations.
    
  21. 
    
  22. * :djadmin:`makemigrations`, which is responsible for creating new migrations
    
  23.   based on the changes you have made to your models.
    
  24. 
    
  25. * :djadmin:`sqlmigrate`, which displays the SQL statements for a migration.
    
  26. 
    
  27. * :djadmin:`showmigrations`, which lists a project's migrations and their
    
  28.   status.
    
  29. 
    
  30. You should think of migrations as a version control system for your database
    
  31. schema. ``makemigrations`` is responsible for packaging up your model changes
    
  32. into individual migration files - analogous to commits - and ``migrate`` is
    
  33. responsible for applying those to your database.
    
  34. 
    
  35. The migration files for each app live in a "migrations" directory inside
    
  36. of that app, and are designed to be committed to, and distributed as part
    
  37. of, its codebase. You should be making them once on your development machine
    
  38. and then running the same migrations on your colleagues' machines, your
    
  39. staging machines, and eventually your production machines.
    
  40. 
    
  41. .. note::
    
  42.     It is possible to override the name of the package which contains the
    
  43.     migrations on a per-app basis by modifying the :setting:`MIGRATION_MODULES`
    
  44.     setting.
    
  45. 
    
  46. Migrations will run the same way on the same dataset and produce consistent
    
  47. results, meaning that what you see in development and staging is, under the
    
  48. same circumstances, exactly what will happen in production.
    
  49. 
    
  50. Django will make migrations for any change to your models or fields - even
    
  51. options that don't affect the database - as the only way it can reconstruct
    
  52. a field correctly is to have all the changes in the history, and you might
    
  53. need those options in some data migrations later on (for example, if you've
    
  54. set custom validators).
    
  55. 
    
  56. Backend Support
    
  57. ===============
    
  58. 
    
  59. Migrations are supported on all backends that Django ships with, as well
    
  60. as any third-party backends if they have programmed in support for schema
    
  61. alteration (done via the :doc:`SchemaEditor </ref/schema-editor>` class).
    
  62. 
    
  63. However, some databases are more capable than others when it comes to
    
  64. schema migrations; some of the caveats are covered below.
    
  65. 
    
  66. PostgreSQL
    
  67. ----------
    
  68. 
    
  69. PostgreSQL is the most capable of all the databases here in terms of schema
    
  70. support.
    
  71. 
    
  72. MySQL
    
  73. -----
    
  74. 
    
  75. MySQL lacks support for transactions around schema alteration operations,
    
  76. meaning that if a migration fails to apply you will have to manually unpick
    
  77. the changes in order to try again (it's impossible to roll back to an
    
  78. earlier point).
    
  79. 
    
  80. In addition, MySQL will fully rewrite tables for almost every schema operation
    
  81. and generally takes a time proportional to the number of rows in the table to
    
  82. add or remove columns. On slower hardware this can be worse than a minute per
    
  83. million rows - adding a few columns to a table with just a few million rows
    
  84. could lock your site up for over ten minutes.
    
  85. 
    
  86. Finally, MySQL has relatively small limits on name lengths for columns, tables
    
  87. and indexes, as well as a limit on the combined size of all columns an index
    
  88. covers. This means that indexes that are possible on other backends will
    
  89. fail to be created under MySQL.
    
  90. 
    
  91. SQLite
    
  92. ------
    
  93. 
    
  94. SQLite has very little built-in schema alteration support, and so Django
    
  95. attempts to emulate it by:
    
  96. 
    
  97. * Creating a new table with the new schema
    
  98. * Copying the data across
    
  99. * Dropping the old table
    
  100. * Renaming the new table to match the original name
    
  101. 
    
  102. This process generally works well, but it can be slow and occasionally
    
  103. buggy. It is not recommended that you run and migrate SQLite in a
    
  104. production environment unless you are very aware of the risks and
    
  105. its limitations; the support Django ships with is designed to allow
    
  106. developers to use SQLite on their local machines to develop less complex
    
  107. Django projects without the need for a full database.
    
  108. 
    
  109. Workflow
    
  110. ========
    
  111. 
    
  112. Django can create migrations for you. Make changes to your models - say, add a
    
  113. field and remove a model - and then run :djadmin:`makemigrations`::
    
  114. 
    
  115.     $ python manage.py makemigrations
    
  116.     Migrations for 'books':
    
  117.       books/migrations/0003_auto.py:
    
  118.         - Alter field author on book
    
  119. 
    
  120. Your models will be scanned and compared to the versions currently
    
  121. contained in your migration files, and then a new set of migrations
    
  122. will be written out. Make sure to read the output to see what
    
  123. ``makemigrations`` thinks you have changed - it's not perfect, and for
    
  124. complex changes it might not be detecting what you expect.
    
  125. 
    
  126. Once you have your new migration files, you should apply them to your
    
  127. database to make sure they work as expected::
    
  128. 
    
  129.     $ python manage.py migrate
    
  130.     Operations to perform:
    
  131.       Apply all migrations: books
    
  132.     Running migrations:
    
  133.       Rendering model states... DONE
    
  134.       Applying books.0003_auto... OK
    
  135. 
    
  136. Once the migration is applied, commit the migration and the models change
    
  137. to your version control system as a single commit - that way, when other
    
  138. developers (or your production servers) check out the code, they'll
    
  139. get both the changes to your models and the accompanying migration at the
    
  140. same time.
    
  141. 
    
  142. If you want to give the migration(s) a meaningful name instead of a generated
    
  143. one, you can use the :option:`makemigrations --name` option::
    
  144. 
    
  145.     $ python manage.py makemigrations --name changed_my_model your_app_label
    
  146. 
    
  147. Version control
    
  148. ---------------
    
  149. 
    
  150. Because migrations are stored in version control, you'll occasionally
    
  151. come across situations where you and another developer have both committed
    
  152. a migration to the same app at the same time, resulting in two migrations
    
  153. with the same number.
    
  154. 
    
  155. Don't worry - the numbers are just there for developers' reference, Django
    
  156. just cares that each migration has a different name. Migrations specify which
    
  157. other migrations they depend on - including earlier migrations in the same
    
  158. app - in the file, so it's possible to detect when there's two new migrations
    
  159. for the same app that aren't ordered.
    
  160. 
    
  161. When this happens, Django will prompt you and give you some options. If it
    
  162. thinks it's safe enough, it will offer to automatically linearize the two
    
  163. migrations for you. If not, you'll have to go in and modify the migrations
    
  164. yourself - don't worry, this isn't difficult, and is explained more in
    
  165. :ref:`migration-files` below.
    
  166. 
    
  167. Transactions
    
  168. ============
    
  169. 
    
  170. On databases that support DDL transactions (SQLite and PostgreSQL), all
    
  171. migration operations will run inside a single transaction by default. In
    
  172. contrast, if a database doesn't support DDL transactions (e.g. MySQL, Oracle)
    
  173. then all operations will run without a transaction.
    
  174. 
    
  175. You can prevent a migration from running in a transaction by setting the
    
  176. ``atomic`` attribute to ``False``. For example::
    
  177. 
    
  178.     from django.db import migrations
    
  179. 
    
  180.     class Migration(migrations.Migration):
    
  181.         atomic = False
    
  182. 
    
  183. It's also possible to execute parts of the migration inside a transaction using
    
  184. :func:`~django.db.transaction.atomic()` or by passing ``atomic=True`` to
    
  185. :class:`~django.db.migrations.operations.RunPython`. See
    
  186. :ref:`non-atomic-migrations` for more details.
    
  187. 
    
  188. Dependencies
    
  189. ============
    
  190. 
    
  191. While migrations are per-app, the tables and relationships implied by
    
  192. your models are too complex to be created for one app at a time. When you make
    
  193. a migration that requires something else to run - for example, you add a
    
  194. ``ForeignKey`` in your ``books`` app to your ``authors`` app - the resulting
    
  195. migration will contain a dependency on a migration in ``authors``.
    
  196. 
    
  197. This means that when you run the migrations, the ``authors`` migration runs
    
  198. first and creates the table the ``ForeignKey`` references, and then the migration
    
  199. that makes the ``ForeignKey`` column runs afterward and creates the constraint.
    
  200. If this didn't happen, the migration would try to create the ``ForeignKey``
    
  201. column without the table it's referencing existing and your database would
    
  202. throw an error.
    
  203. 
    
  204. This dependency behavior affects most migration operations where you
    
  205. restrict to a single app. Restricting to a single app (either in
    
  206. ``makemigrations`` or ``migrate``) is a best-efforts promise, and not
    
  207. a guarantee; any other apps that need to be used to get dependencies correct
    
  208. will be.
    
  209. 
    
  210. Apps without migrations must not have relations (``ForeignKey``,
    
  211. ``ManyToManyField``, etc.) to apps with migrations. Sometimes it may work, but
    
  212. it's not supported.
    
  213. 
    
  214. .. _migration-files:
    
  215. 
    
  216. Migration files
    
  217. ===============
    
  218. 
    
  219. Migrations are stored as an on-disk format, referred to here as
    
  220. "migration files". These files are actually normal Python files with an
    
  221. agreed-upon object layout, written in a declarative style.
    
  222. 
    
  223. A basic migration file looks like this::
    
  224. 
    
  225.     from django.db import migrations, models
    
  226. 
    
  227.     class Migration(migrations.Migration):
    
  228. 
    
  229.         dependencies = [('migrations', '0001_initial')]
    
  230. 
    
  231.         operations = [
    
  232.             migrations.DeleteModel('Tribble'),
    
  233.             migrations.AddField('Author', 'rating', models.IntegerField(default=0)),
    
  234.         ]
    
  235. 
    
  236. What Django looks for when it loads a migration file (as a Python module) is
    
  237. a subclass of ``django.db.migrations.Migration`` called ``Migration``. It then
    
  238. inspects this object for four attributes, only two of which are used
    
  239. most of the time:
    
  240. 
    
  241. * ``dependencies``, a list of migrations this one depends on.
    
  242. * ``operations``, a list of ``Operation`` classes that define what this
    
  243.   migration does.
    
  244. 
    
  245. The operations are the key; they are a set of declarative instructions which
    
  246. tell Django what schema changes need to be made. Django scans them and
    
  247. builds an in-memory representation of all of the schema changes to all apps,
    
  248. and uses this to generate the SQL which makes the schema changes.
    
  249. 
    
  250. That in-memory structure is also used to work out what the differences are
    
  251. between your models and the current state of your migrations; Django runs
    
  252. through all the changes, in order, on an in-memory set of models to come
    
  253. up with the state of your models last time you ran ``makemigrations``. It
    
  254. then uses these models to compare against the ones in your ``models.py`` files
    
  255. to work out what you have changed.
    
  256. 
    
  257. You should rarely, if ever, need to edit migration files by hand, but
    
  258. it's entirely possible to write them manually if you need to. Some of the
    
  259. more complex operations are not autodetectable and are only available via
    
  260. a hand-written migration, so don't be scared about editing them if you have to.
    
  261. 
    
  262. Custom fields
    
  263. -------------
    
  264. 
    
  265. You can't modify the number of positional arguments in an already migrated
    
  266. custom field without raising a ``TypeError``. The old migration will call the
    
  267. modified ``__init__`` method with the old signature. So if you need a new
    
  268. argument, please create a keyword argument and add something like
    
  269. ``assert 'argument_name' in kwargs`` in the constructor.
    
  270. 
    
  271. .. _using-managers-in-migrations:
    
  272. 
    
  273. Model managers
    
  274. --------------
    
  275. 
    
  276. You can optionally serialize managers into migrations and have them available
    
  277. in :class:`~django.db.migrations.operations.RunPython` operations. This is done
    
  278. by defining a ``use_in_migrations`` attribute on the manager class::
    
  279. 
    
  280.     class MyManager(models.Manager):
    
  281.         use_in_migrations = True
    
  282. 
    
  283.     class MyModel(models.Model):
    
  284.         objects = MyManager()
    
  285. 
    
  286. If you are using the :meth:`~django.db.models.from_queryset` function to
    
  287. dynamically generate a manager class, you need to inherit from the generated
    
  288. class to make it importable::
    
  289. 
    
  290.     class MyManager(MyBaseManager.from_queryset(CustomQuerySet)):
    
  291.         use_in_migrations = True
    
  292. 
    
  293.     class MyModel(models.Model):
    
  294.         objects = MyManager()
    
  295. 
    
  296. Please refer to the notes about :ref:`historical-models` in migrations to see
    
  297. the implications that come along.
    
  298. 
    
  299. Initial migrations
    
  300. ------------------
    
  301. 
    
  302. .. attribute:: Migration.initial
    
  303. 
    
  304. The "initial migrations" for an app are the migrations that create the first
    
  305. version of that app's tables. Usually an app will have one initial migration,
    
  306. but in some cases of complex model interdependencies it may have two or more.
    
  307. 
    
  308. Initial migrations are marked with an ``initial = True`` class attribute on the
    
  309. migration class. If an ``initial`` class attribute isn't found, a migration
    
  310. will be considered "initial" if it is the first migration in the app (i.e. if
    
  311. it has no dependencies on any other migration in the same app).
    
  312. 
    
  313. When the :option:`migrate --fake-initial` option is used, these initial
    
  314. migrations are treated specially. For an initial migration that creates one or
    
  315. more tables (``CreateModel`` operation), Django checks that all of those tables
    
  316. already exist in the database and fake-applies the migration if so. Similarly,
    
  317. for an initial migration that adds one or more fields (``AddField`` operation),
    
  318. Django checks that all of the respective columns already exist in the database
    
  319. and fake-applies the migration if so. Without ``--fake-initial``, initial
    
  320. migrations are treated no differently from any other migration.
    
  321. 
    
  322. .. _migration-history-consistency:
    
  323. 
    
  324. History consistency
    
  325. -------------------
    
  326. 
    
  327. As previously discussed, you may need to linearize migrations manually when two
    
  328. development branches are joined. While editing migration dependencies, you can
    
  329. inadvertently create an inconsistent history state where a migration has been
    
  330. applied but some of its dependencies haven't. This is a strong indication that
    
  331. the dependencies are incorrect, so Django will refuse to run migrations or make
    
  332. new migrations until it's fixed. When using multiple databases, you can use the
    
  333. :meth:`allow_migrate` method of :ref:`database routers
    
  334. <topics-db-multi-db-routing>` to control which databases
    
  335. :djadmin:`makemigrations` checks for consistent history.
    
  336. 
    
  337. Adding migrations to apps
    
  338. =========================
    
  339. 
    
  340. New apps come preconfigured to accept migrations, and so you can add migrations
    
  341. by running :djadmin:`makemigrations` once you've made some changes.
    
  342. 
    
  343. If your app already has models and database tables, and doesn't have migrations
    
  344. yet (for example, you created it against a previous Django version), you'll
    
  345. need to convert it to use migrations by running::
    
  346. 
    
  347.     $ python manage.py makemigrations your_app_label
    
  348. 
    
  349. This will make a new initial migration for your app. Now, run ``python
    
  350. manage.py migrate --fake-initial``, and Django will detect that you have an
    
  351. initial migration *and* that the tables it wants to create already exist, and
    
  352. will mark the migration as already applied. (Without the :option:`migrate
    
  353. --fake-initial` flag, the command would error out because the tables it wants
    
  354. to create already exist.)
    
  355. 
    
  356. Note that this only works given two things:
    
  357. 
    
  358. * You have not changed your models since you made their tables. For migrations
    
  359.   to work, you must make the initial migration *first* and then make changes,
    
  360.   as Django compares changes against migration files, not the database.
    
  361. 
    
  362. * You have not manually edited your database - Django won't be able to detect
    
  363.   that your database doesn't match your models, you'll just get errors when
    
  364.   migrations try to modify those tables.
    
  365. 
    
  366. .. _reversing-migrations:
    
  367. 
    
  368. Reversing migrations
    
  369. ====================
    
  370. 
    
  371. Migrations can be reversed with :djadmin:`migrate` by passing the number of the
    
  372. previous migration. For example, to reverse migration ``books.0003``:
    
  373. 
    
  374. .. console::
    
  375. 
    
  376.     $ python manage.py migrate books 0002
    
  377.     Operations to perform:
    
  378.       Target specific migration: 0002_auto, from books
    
  379.     Running migrations:
    
  380.       Rendering model states... DONE
    
  381.       Unapplying books.0003_auto... OK
    
  382. 
    
  383. If you want to reverse all migrations applied for an app, use the name
    
  384. ``zero``:
    
  385. 
    
  386. .. console::
    
  387. 
    
  388.     $ python manage.py migrate books zero
    
  389.     Operations to perform:
    
  390.       Unapply all migrations: books
    
  391.     Running migrations:
    
  392.       Rendering model states... DONE
    
  393.       Unapplying books.0002_auto... OK
    
  394.       Unapplying books.0001_initial... OK
    
  395. 
    
  396. A migration is irreversible if it contains any irreversible operations.
    
  397. Attempting to reverse such migrations will raise ``IrreversibleError``:
    
  398. 
    
  399. .. console::
    
  400. 
    
  401.     $ python manage.py migrate books 0002
    
  402.     Operations to perform:
    
  403.       Target specific migration: 0002_auto, from books
    
  404.     Running migrations:
    
  405.       Rendering model states... DONE
    
  406.       Unapplying books.0003_auto...Traceback (most recent call last):
    
  407.     django.db.migrations.exceptions.IrreversibleError: Operation <RunSQL  sql='DROP TABLE demo_books'> in books.0003_auto is not reversible
    
  408. 
    
  409. .. _historical-models:
    
  410. 
    
  411. Historical models
    
  412. =================
    
  413. 
    
  414. When you run migrations, Django is working from historical versions of your
    
  415. models stored in the migration files. If you write Python code using the
    
  416. :class:`~django.db.migrations.operations.RunPython` operation, or if you have
    
  417. ``allow_migrate`` methods on your database routers, you **need to use** these
    
  418. historical model versions rather than importing them directly.
    
  419. 
    
  420. .. warning::
    
  421. 
    
  422.   If you import models directly rather than using the historical models,
    
  423.   your migrations *may work initially* but will fail in the future when you
    
  424.   try to rerun old migrations (commonly, when you set up a new installation
    
  425.   and run through all the migrations to set up the database).
    
  426. 
    
  427.   This means that historical model problems may not be immediately obvious.
    
  428.   If you run into this kind of failure, it's OK to edit the migration to use
    
  429.   the historical models rather than direct imports and commit those changes.
    
  430. 
    
  431. Because it's impossible to serialize arbitrary Python code, these historical
    
  432. models will not have any custom methods that you have defined. They will,
    
  433. however, have the same fields, relationships, managers (limited to those with
    
  434. ``use_in_migrations = True``) and ``Meta`` options (also versioned, so they may
    
  435. be different from your current ones).
    
  436. 
    
  437. .. warning::
    
  438. 
    
  439.   This means that you will NOT have custom ``save()`` methods called on objects
    
  440.   when you access them in migrations, and you will NOT have any custom
    
  441.   constructors or instance methods. Plan appropriately!
    
  442. 
    
  443. References to functions in field options such as ``upload_to`` and
    
  444. ``limit_choices_to`` and model manager declarations with managers having
    
  445. ``use_in_migrations = True`` are serialized in migrations, so the functions and
    
  446. classes will need to be kept around for as long as there is a migration
    
  447. referencing them. Any :doc:`custom model fields </howto/custom-model-fields>`
    
  448. will also need to be kept, since these are imported directly by migrations.
    
  449. 
    
  450. In addition, the concrete base classes of the model are stored as pointers, so
    
  451. you must always keep base classes around for as long as there is a migration
    
  452. that contains a reference to them. On the plus side, methods and managers from
    
  453. these base classes inherit normally, so if you absolutely need access to these
    
  454. you can opt to move them into a superclass.
    
  455. 
    
  456. To remove old references, you can :ref:`squash migrations <migration-squashing>`
    
  457. or, if there aren't many references, copy them into the migration files.
    
  458. 
    
  459. .. _migrations-removing-model-fields:
    
  460. 
    
  461. Considerations when removing model fields
    
  462. =========================================
    
  463. 
    
  464. Similar to the "references to historical functions" considerations described in
    
  465. the previous section, removing custom model fields from your project or
    
  466. third-party app will cause a problem if they are referenced in old migrations.
    
  467. 
    
  468. To help with this situation, Django provides some model field attributes to
    
  469. assist with model field deprecation using the :doc:`system checks framework
    
  470. </topics/checks>`.
    
  471. 
    
  472. Add the ``system_check_deprecated_details`` attribute to your model field
    
  473. similar to the following::
    
  474. 
    
  475.     class IPAddressField(Field):
    
  476.         system_check_deprecated_details = {
    
  477.             'msg': (
    
  478.                 'IPAddressField has been deprecated. Support for it (except '
    
  479.                 'in historical migrations) will be removed in Django 1.9.'
    
  480.             ),
    
  481.             'hint': 'Use GenericIPAddressField instead.',  # optional
    
  482.             'id': 'fields.W900',  # pick a unique ID for your field.
    
  483.         }
    
  484. 
    
  485. After a deprecation period of your choosing (two or three feature releases for
    
  486. fields in Django itself), change the ``system_check_deprecated_details``
    
  487. attribute to ``system_check_removed_details`` and update the dictionary similar
    
  488. to::
    
  489. 
    
  490.     class IPAddressField(Field):
    
  491.         system_check_removed_details = {
    
  492.             'msg': (
    
  493.                 'IPAddressField has been removed except for support in '
    
  494.                 'historical migrations.'
    
  495.             ),
    
  496.             'hint': 'Use GenericIPAddressField instead.',
    
  497.             'id': 'fields.E900',  # pick a unique ID for your field.
    
  498.         }
    
  499. 
    
  500. You should keep the field's methods that are required for it to operate in
    
  501. database migrations such as ``__init__()``, ``deconstruct()``, and
    
  502. ``get_internal_type()``. Keep this stub field for as long as any migrations
    
  503. which reference the field exist. For example, after squashing migrations and
    
  504. removing the old ones, you should be able to remove the field completely.
    
  505. 
    
  506. .. _data-migrations:
    
  507. 
    
  508. Data Migrations
    
  509. ===============
    
  510. 
    
  511. As well as changing the database schema, you can also use migrations to change
    
  512. the data in the database itself, in conjunction with the schema if you want.
    
  513. 
    
  514. Migrations that alter data are usually called "data migrations"; they're best
    
  515. written as separate migrations, sitting alongside your schema migrations.
    
  516. 
    
  517. Django can't automatically generate data migrations for you, as it does with
    
  518. schema migrations, but it's not very hard to write them. Migration files in
    
  519. Django are made up of :doc:`Operations </ref/migration-operations>`, and
    
  520. the main operation you use for data migrations is
    
  521. :class:`~django.db.migrations.operations.RunPython`.
    
  522. 
    
  523. To start, make an empty migration file you can work from (Django will put
    
  524. the file in the right place, suggest a name, and add dependencies for you)::
    
  525. 
    
  526.     python manage.py makemigrations --empty yourappname
    
  527. 
    
  528. Then, open up the file; it should look something like this::
    
  529. 
    
  530.     # Generated by Django A.B on YYYY-MM-DD HH:MM
    
  531.     from django.db import migrations
    
  532. 
    
  533.     class Migration(migrations.Migration):
    
  534. 
    
  535.         dependencies = [
    
  536.             ('yourappname', '0001_initial'),
    
  537.         ]
    
  538. 
    
  539.         operations = [
    
  540.         ]
    
  541. 
    
  542. Now, all you need to do is create a new function and have
    
  543. :class:`~django.db.migrations.operations.RunPython` use it.
    
  544. :class:`~django.db.migrations.operations.RunPython` expects a callable as its argument
    
  545. which takes two arguments - the first is an :doc:`app registry
    
  546. </ref/applications/>` that has the historical versions of all your models
    
  547. loaded into it to match where in your history the migration sits, and the
    
  548. second is a :doc:`SchemaEditor </ref/schema-editor>`, which you can use to
    
  549. manually effect database schema changes (but beware, doing this can confuse
    
  550. the migration autodetector!)
    
  551. 
    
  552. Let's write a migration that populates our new ``name`` field with the combined
    
  553. values of ``first_name`` and ``last_name`` (we've come to our senses and
    
  554. realized that not everyone has first and last names). All we need to do is use
    
  555. the historical model and iterate over the rows::
    
  556. 
    
  557.     from django.db import migrations
    
  558. 
    
  559.     def combine_names(apps, schema_editor):
    
  560.         # We can't import the Person model directly as it may be a newer
    
  561.         # version than this migration expects. We use the historical version.
    
  562.         Person = apps.get_model('yourappname', 'Person')
    
  563.         for person in Person.objects.all():
    
  564.             person.name = '%s %s' % (person.first_name, person.last_name)
    
  565.             person.save()
    
  566. 
    
  567.     class Migration(migrations.Migration):
    
  568. 
    
  569.         dependencies = [
    
  570.             ('yourappname', '0001_initial'),
    
  571.         ]
    
  572. 
    
  573.         operations = [
    
  574.             migrations.RunPython(combine_names),
    
  575.         ]
    
  576. 
    
  577. Once that's done, we can run ``python manage.py migrate`` as normal and the
    
  578. data migration will run in place alongside other migrations.
    
  579. 
    
  580. You can pass a second callable to
    
  581. :class:`~django.db.migrations.operations.RunPython` to run whatever logic you
    
  582. want executed when migrating backwards. If this callable is omitted, migrating
    
  583. backwards will raise an exception.
    
  584. 
    
  585. Accessing models from other apps
    
  586. --------------------------------
    
  587. 
    
  588. When writing a ``RunPython`` function that uses models from apps other than the
    
  589. one in which the migration is located, the migration's ``dependencies``
    
  590. attribute should include the latest migration of each app that is involved,
    
  591. otherwise you may get an error similar to: ``LookupError: No installed app
    
  592. with label 'myappname'`` when you try to retrieve the model in the ``RunPython``
    
  593. function using ``apps.get_model()``.
    
  594. 
    
  595. In the following example, we have a migration in ``app1`` which needs to use
    
  596. models in ``app2``. We aren't concerned with the details of ``move_m1`` other
    
  597. than the fact it will need to access models from both apps. Therefore we've
    
  598. added a dependency that specifies the last migration of ``app2``::
    
  599. 
    
  600.     class Migration(migrations.Migration):
    
  601. 
    
  602.         dependencies = [
    
  603.             ('app1', '0001_initial'),
    
  604.             # added dependency to enable using models from app2 in move_m1
    
  605.             ('app2', '0004_foobar'),
    
  606.         ]
    
  607. 
    
  608.         operations = [
    
  609.             migrations.RunPython(move_m1),
    
  610.         ]
    
  611. 
    
  612. More advanced migrations
    
  613. ------------------------
    
  614. 
    
  615. If you're interested in the more advanced migration operations, or want
    
  616. to be able to write your own, see the :doc:`migration operations reference
    
  617. </ref/migration-operations>` and the "how-to" on :doc:`writing migrations
    
  618. </howto/writing-migrations>`.
    
  619. 
    
  620. .. _migration-squashing:
    
  621. 
    
  622. Squashing migrations
    
  623. ====================
    
  624. 
    
  625. You are encouraged to make migrations freely and not worry about how many you
    
  626. have; the migration code is optimized to deal with hundreds at a time without
    
  627. much slowdown. However, eventually you will want to move back from having
    
  628. several hundred migrations to just a few, and that's where squashing comes in.
    
  629. 
    
  630. Squashing is the act of reducing an existing set of many migrations down to
    
  631. one (or sometimes a few) migrations which still represent the same changes.
    
  632. 
    
  633. Django does this by taking all of your existing migrations, extracting their
    
  634. ``Operation``\s and putting them all in sequence, and then running an optimizer
    
  635. over them to try and reduce the length of the list - for example, it knows
    
  636. that :class:`~django.db.migrations.operations.CreateModel` and
    
  637. :class:`~django.db.migrations.operations.DeleteModel` cancel each other out,
    
  638. and it knows that :class:`~django.db.migrations.operations.AddField` can be
    
  639. rolled into :class:`~django.db.migrations.operations.CreateModel`.
    
  640. 
    
  641. Once the operation sequence has been reduced as much as possible - the amount
    
  642. possible depends on how closely intertwined your models are and if you have
    
  643. any :class:`~django.db.migrations.operations.RunSQL`
    
  644. or :class:`~django.db.migrations.operations.RunPython` operations (which can't
    
  645. be optimized through unless they are marked as ``elidable``) - Django will then
    
  646. write it back out into a new set of migration files.
    
  647. 
    
  648. These files are marked to say they replace the previously-squashed migrations,
    
  649. so they can coexist with the old migration files, and Django will intelligently
    
  650. switch between them depending where you are in the history. If you're still
    
  651. part-way through the set of migrations that you squashed, it will keep using
    
  652. them until it hits the end and then switch to the squashed history, while new
    
  653. installs will use the new squashed migration and skip all the old ones.
    
  654. 
    
  655. This enables you to squash and not mess up systems currently in production
    
  656. that aren't fully up-to-date yet. The recommended process is to squash, keeping
    
  657. the old files, commit and release, wait until all systems are upgraded with
    
  658. the new release (or if you're a third-party project, ensure your users upgrade
    
  659. releases in order without skipping any), and then remove the old files, commit
    
  660. and do a second release.
    
  661. 
    
  662. The command that backs all this is :djadmin:`squashmigrations` - pass it the
    
  663. app label and migration name you want to squash up to, and it'll get to work::
    
  664. 
    
  665.   $ ./manage.py squashmigrations myapp 0004
    
  666.   Will squash the following migrations:
    
  667.    - 0001_initial
    
  668.    - 0002_some_change
    
  669.    - 0003_another_change
    
  670.    - 0004_undo_something
    
  671.   Do you wish to proceed? [yN] y
    
  672.   Optimizing...
    
  673.     Optimized from 12 operations to 7 operations.
    
  674.   Created new squashed migration /home/andrew/Programs/DjangoTest/test/migrations/0001_squashed_0004_undo_something.py
    
  675.     You should commit this migration but leave the old ones in place;
    
  676.     the new migration will be used for new installs. Once you are sure
    
  677.     all instances of the codebase have applied the migrations you squashed,
    
  678.     you can delete them.
    
  679. 
    
  680. Use the :option:`squashmigrations --squashed-name` option if you want to set
    
  681. the name of the squashed migration rather than use an autogenerated one.
    
  682. 
    
  683. Note that model interdependencies in Django can get very complex, and squashing
    
  684. may result in migrations that do not run; either mis-optimized (in which case
    
  685. you can try again with ``--no-optimize``, though you should also report an issue),
    
  686. or with a ``CircularDependencyError``, in which case you can manually resolve it.
    
  687. 
    
  688. To manually resolve a ``CircularDependencyError``, break out one of
    
  689. the ForeignKeys in the circular dependency loop into a separate
    
  690. migration, and move the dependency on the other app with it. If you're unsure,
    
  691. see how :djadmin:`makemigrations` deals with the problem when asked to create
    
  692. brand new migrations from your models. In a future release of Django,
    
  693. :djadmin:`squashmigrations` will be updated to attempt to resolve these errors
    
  694. itself.
    
  695. 
    
  696. Once you've squashed your migration, you should then commit it alongside the
    
  697. migrations it replaces and distribute this change to all running instances
    
  698. of your application, making sure that they run ``migrate`` to store the change
    
  699. in their database.
    
  700. 
    
  701. You must then transition the squashed migration to a normal migration by:
    
  702. 
    
  703. - Deleting all the migration files it replaces.
    
  704. - Updating all migrations that depend on the deleted migrations to depend on
    
  705.   the squashed migration instead.
    
  706. - Removing the ``replaces`` attribute in the ``Migration`` class of the
    
  707.   squashed migration (this is how Django tells that it is a squashed migration).
    
  708. 
    
  709. .. note::
    
  710.     Once you've squashed a migration, you should not then re-squash that squashed
    
  711.     migration until you have fully transitioned it to a normal migration.
    
  712. 
    
  713. .. admonition:: Pruning references to deleted migrations
    
  714. 
    
  715.     .. versionadded:: 4.1
    
  716. 
    
  717.     If it is likely that you may reuse the name of a deleted migration in the
    
  718.     future, you should remove references to it from Django’s migrations table
    
  719.     with the :option:`migrate --prune` option.
    
  720. 
    
  721. .. _migration-serializing:
    
  722. 
    
  723. Serializing values
    
  724. ==================
    
  725. 
    
  726. Migrations are Python files containing the old definitions of your models
    
  727. - thus, to write them, Django must take the current state of your models and
    
  728. serialize them out into a file.
    
  729. 
    
  730. While Django can serialize most things, there are some things that we just
    
  731. can't serialize out into a valid Python representation - there's no Python
    
  732. standard for how a value can be turned back into code (``repr()`` only works
    
  733. for basic values, and doesn't specify import paths).
    
  734. 
    
  735. Django can serialize the following:
    
  736. 
    
  737. - ``int``, ``float``, ``bool``, ``str``, ``bytes``, ``None``, ``NoneType``
    
  738. - ``list``, ``set``, ``tuple``, ``dict``, ``range``.
    
  739. - ``datetime.date``, ``datetime.time``, and ``datetime.datetime`` instances
    
  740.   (include those that are timezone-aware)
    
  741. - ``decimal.Decimal`` instances
    
  742. - ``enum.Enum`` instances
    
  743. - ``uuid.UUID`` instances
    
  744. - :func:`functools.partial` and :class:`functools.partialmethod` instances
    
  745.   which have serializable ``func``, ``args``, and ``keywords`` values.
    
  746. - Pure and concrete path objects from :mod:`pathlib`. Concrete paths are
    
  747.   converted to their pure path equivalent, e.g. :class:`pathlib.PosixPath` to
    
  748.   :class:`pathlib.PurePosixPath`.
    
  749. - :class:`os.PathLike` instances, e.g. :class:`os.DirEntry`, which are
    
  750.   converted to ``str`` or ``bytes`` using :func:`os.fspath`.
    
  751. - ``LazyObject`` instances which wrap a serializable value.
    
  752. - Enumeration types (e.g. ``TextChoices`` or ``IntegerChoices``) instances.
    
  753. - Any Django field
    
  754. - Any function or method reference (e.g. ``datetime.datetime.today``) (must be in module's top-level scope)
    
  755. - Unbound methods used from within the class body
    
  756. - Any class reference (must be in module's top-level scope)
    
  757. - Anything with a custom ``deconstruct()`` method (:ref:`see below <custom-deconstruct-method>`)
    
  758. 
    
  759. Django cannot serialize:
    
  760. 
    
  761. - Nested classes
    
  762. - Arbitrary class instances (e.g. ``MyClass(4.3, 5.7)``)
    
  763. - Lambdas
    
  764. 
    
  765. .. _custom-migration-serializers:
    
  766. 
    
  767. Custom serializers
    
  768. ------------------
    
  769. 
    
  770. You can serialize other types by writing a custom serializer. For example, if
    
  771. Django didn't serialize :class:`~decimal.Decimal` by default, you could do
    
  772. this::
    
  773. 
    
  774.     from decimal import Decimal
    
  775. 
    
  776.     from django.db.migrations.serializer import BaseSerializer
    
  777.     from django.db.migrations.writer import MigrationWriter
    
  778. 
    
  779.     class DecimalSerializer(BaseSerializer):
    
  780.         def serialize(self):
    
  781.             return repr(self.value), {'from decimal import Decimal'}
    
  782. 
    
  783.     MigrationWriter.register_serializer(Decimal, DecimalSerializer)
    
  784. 
    
  785. The first argument of ``MigrationWriter.register_serializer()`` is a type or
    
  786. iterable of types that should use the serializer.
    
  787. 
    
  788. The ``serialize()`` method of your serializer must return a string of how the
    
  789. value should appear in migrations and a set of any imports that are needed in
    
  790. the migration.
    
  791. 
    
  792. .. _custom-deconstruct-method:
    
  793. 
    
  794. Adding a ``deconstruct()`` method
    
  795. ---------------------------------
    
  796. 
    
  797. You can let Django serialize your own custom class instances by giving the class
    
  798. a ``deconstruct()`` method. It takes no arguments, and should return a tuple
    
  799. of three things ``(path, args, kwargs)``:
    
  800. 
    
  801. * ``path`` should be the Python path to the class, with the class name included
    
  802.   as the last part (for example, ``myapp.custom_things.MyClass``). If your
    
  803.   class is not available at the top level of a module it is not serializable.
    
  804. 
    
  805. * ``args`` should be a list of positional arguments to pass to your class'
    
  806.   ``__init__`` method. Everything in this list should itself be serializable.
    
  807. 
    
  808. * ``kwargs`` should be a dict of keyword arguments to pass to your class'
    
  809.   ``__init__`` method. Every value should itself be serializable.
    
  810. 
    
  811. .. note::
    
  812. 
    
  813.     This return value is different from the ``deconstruct()`` method
    
  814.     :ref:`for custom fields <custom-field-deconstruct-method>` which returns a
    
  815.     tuple of four items.
    
  816. 
    
  817. Django will write out the value as an instantiation of your class with the
    
  818. given arguments, similar to the way it writes out references to Django fields.
    
  819. 
    
  820. To prevent a new migration from being created each time
    
  821. :djadmin:`makemigrations` is run, you should also add a ``__eq__()`` method to
    
  822. the decorated class. This function will be called by Django's migration
    
  823. framework to detect changes between states.
    
  824. 
    
  825. As long as all of the arguments to your class' constructor are themselves
    
  826. serializable, you can use the ``@deconstructible`` class decorator from
    
  827. ``django.utils.deconstruct`` to add the ``deconstruct()`` method::
    
  828. 
    
  829.     from django.utils.deconstruct import deconstructible
    
  830. 
    
  831.     @deconstructible
    
  832.     class MyCustomClass:
    
  833. 
    
  834.         def __init__(self, foo=1):
    
  835.             self.foo = foo
    
  836.             ...
    
  837. 
    
  838.         def __eq__(self, other):
    
  839.             return self.foo == other.foo
    
  840. 
    
  841. 
    
  842. The decorator adds logic to capture and preserve the arguments on their
    
  843. way into your constructor, and then returns those arguments exactly when
    
  844. deconstruct() is called.
    
  845. 
    
  846. Supporting multiple Django versions
    
  847. ===================================
    
  848. 
    
  849. If you are the maintainer of a third-party app with models, you may need to
    
  850. ship migrations that support multiple Django versions. In this case, you should
    
  851. always run :djadmin:`makemigrations` **with the lowest Django version you wish
    
  852. to support**.
    
  853. 
    
  854. The migrations system will maintain backwards-compatibility according to the
    
  855. same policy as the rest of Django, so migration files generated on Django X.Y
    
  856. should run unchanged on Django X.Y+1. The migrations system does not promise
    
  857. forwards-compatibility, however. New features may be added, and migration files
    
  858. generated with newer versions of Django may not work on older versions.
    
  859. 
    
  860. .. seealso::
    
  861. 
    
  862.     :doc:`The Migrations Operations Reference </ref/migration-operations>`
    
  863.         Covers the schema operations API, special operations, and writing your
    
  864.         own operations.
    
  865. 
    
  866.     :doc:`The Writing Migrations "how-to" </howto/writing-migrations>`
    
  867.         Explains how to structure and write database migrations for different
    
  868.         scenarios you might encounter.