liveBook · Manning

Migrate command

As explained in Django in Action

The Django migrate command applies available migration scripts to synchronize a database with the application's migration state.

`migrate` is Django’s management command for bringing the database into line with available migration scripts. A migration is a Python script describing database changes; `migrate` runs scripts that describe model changes not applied during an earlier invocation. It handles Django’s built-in apps as well as project apps, so the initial warning about unapplied migrations is a database setup task, not a problem with a view. section 2.7 section 4.12

The usual workflow has two distinct steps. First, change a model and run `python manage.py makemigrations` to create a migration file. Then run `python manage.py migrate` to apply that file to the database. Updating a `Model` class alone changes Python code, not the corresponding database table—sadly, databases do not read your mind. section 4.12 section 4.7.1

Use `migrate` when you need to:

  • Set up tables required by Django’s built-in apps, such as admin, authentication, content types, and sessions. section 2.7
  • Apply a newly generated migration after adding models, fields, or relationships. section 4.7.1 section 7.4.2
  • Confirm which migration scripts ran by reading the command output, where a successful operation ends in `OK`. section 2.7 section 4.7.1

Adding venue rooms

After defining `Venue` and `Room`, including a `ForeignKey` from each room to its venue, run `python manage.py makemigrations bands`. Django creates `bands/migrations/0002_venue_room.py`; running `python manage.py migrate` then applies that migration and creates the needed database structure. Only after this synchronization can the shell create a venue and its rooms. section 4.7.1

Operations to perform:
  Apply all migrations: admin, auth, bands, contenttypes, sessions
Running migrations:
  Applying bands.0002_venue_room... OK

This output shows `migrate` discovering pending migrations across installed apps and successfully applying the `bands.0002_venue_room` script. section 4.7.1

For a fresh `promoters` app, the same pattern starts with an initial migration: create the `Promoter` model, generate `promoters.0001_initial.py`, and run `migrate`. Repeating this cycle as models evolve makes database changes explicit and ordered rather than accidental. section 10.1 section 4.12

How Django in Action covers it

How deep it goes
Explains the generate-then-apply workflow and command output, while deferring migration-system details.
Examples
Demonstrates initial built-in migrations, Venue and Room tables, Promoter creation, and added Venue upload fields.
What it assumes
Assumes a Django project and model changes while introducing database synchronization through management commands.
What you can do after
Apply it via Django’s migrate command to synchronize generated model-change migrations with a database.
How it connects
Connects Model classes, the Django ORM, makemigrations, ForeignKey relationships, and SQLite databases.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.