# Applying Migrations — Django / FastAPI

Source: https://www.skillbyai.com/en/django-fastapi/m-migrate

> migrate brings the database up to date.

## Tracked in django_migrations

`python manage.py migrate` applies unapplied migrations in dependency order and records them in the django_migrations table, so each runs once per database. Built-in apps (auth, admin, sessions, contenttypes) bring their own migrations. In deployment, run migrate as a separate step before (or as part of) releasing new code, and design changes to be backward compatible with the previous code version during rollout.

## Applying migrations, run

I ran this with Django 6.1.1 and Python 3.12 in a demo project (shopsite with a catalog app) using SQLite. Output trimmed to the plan, the first auth migration and the catalog migration; every migration reported OK.

```bash
python manage.py migrate
```

Output:

```
Operations to perform:
  Apply all migrations: admin, auth, catalog, contenttypes, sessions
  Applying auth.0001_initial... OK
  Applying catalog.0001_initial... OK
```

## Expand, then contract

Add new columns first and remove old ones in a later release, so old and new code can run during the rollout.

**Quiz:** How does Django know which migrations already ran?

- [ ] It reruns all migrations every time
- [ ] It reads file timestamps
- [ ] It asks the user
- [x] It records them in the django_migrations table

*Answer:* It records them in the django_migrations table. Applied migrations are tracked.
