liveBook · Manning

Makemigrations

As explained in Django in Action

makemigrations is a Django management command that generates migration files from changes to application models.

`makemigrations` is Django’s management command for turning model changes into migration scripts: Python files that describe the database changes needed to keep tables aligned with ORM models. It creates the migration plan; `migrate` performs that plan against the database. section 4.12 section 10.6

A model class is only a Python-side proxy for a database table. Adding a field or a model does not alter the database by itself—no tiny database elves receive the memo. Run `makemigrations` after changing models, then run `migrate` to apply any newly created, outstanding scripts. section 4.2 section 4.7.1

(venv) RiffMates$ python manage.py makemigrations bands

Run the command with an app label to generate migrations for that app. For a new app, Django creates a `migrations` directory and an initial script; later runs detect model changes and add further scripts. section 4.2

The usual workflow is short:

  • Define or change a class inheriting from `django.db.models.Model`. section 4.12
  • Run `python manage.py makemigrations <app>` to generate a script describing the detected changes. section 4.2
  • Inspect the reported migration, such as an initial `Create model Musician` operation. section 4.2
  • Run `python manage.py migrate` to execute outstanding migration scripts on the database. section 4.2

Adding related venue data

After adding `Venue` and `Room` models, with `Room.venue` declared as a `ForeignKey`, running `python manage.py makemigrations bands` produces `0002_venue_room.py`. Its output reports creation of both models. Running `migrate` then synchronizes those additions to the database before Venue and Room objects are created in the Django shell. section 4.7.1 section 4.7.1

Generated scripts are project code, not disposable command output. An initial migration is commonly named `0001_initial.py`; later filenames use an incremented number and a hint about the changed model or field. Django orders execution from each migration’s dependencies rather than treating the filename number as meaningful framework data. section 10.6

A migration declares a `Migration` class with an initial-state flag, dependencies, and operations. Operations such as `CreateModel` describe schema work and use the same field classes found in models. When a change affects existing rows, `makemigrations` can prompt for a default value and provide a REPL for entering it. section 10.6

`makemigrations --empty` creates a migration stub with no operations. Migration scripts can also run forward or backward, and a `RunPython` operation can call functions for database side effects in each direction. section 10.6

How Django in Action covers it

How deep it goes
Explains generated filenames, Migration fields, dependencies, operations, default prompts, empty stubs, and reversible `RunPython` work.
Examples
Demonstrates initial Musician and Promoter migrations, then a Venue–Room ForeignKey migration named `0002_venue_room.py`.
What it assumes
Introduces migrations from model classes and tables while assuming a Django app has been created and registered in `INSTALLED_APPS`.
What you can do after
Operate/configure it by generating model-change scripts, supplying defaults when prompted, and applying the resulting migrations with `migrate`.
How it connects
Connects Django ORM models, database tables, `migrate`, ForeignKey relationships, Migration operations, and `RunPython`.

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.