0Pricing
Django Academy · Lesson

sqlmigrate and Inspecting SQL

See the raw SQL a migration will run.

sqlmigrate and Inspecting SQL is a free Django Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

See the Real SQL

Migrations are Python, but the database speaks SQL. The sqlmigrate command shows the exact SQL a migration will run.

How to Run It

Give sqlmigrate an app label and a migration number, and it prints the SQL without applying anything.

python manage.py sqlmigrate blog 0002

It Does Not Apply

sqlmigrate is read-only. It prints the statements so you can review them, never touching your database.

Reading CREATE TABLE

An initial migration shows a CREATE TABLE with each column, type, and constraint laid out in plain SQL.

CREATE TABLE "blog_post" (
  "id" integer PRIMARY KEY AUTOINCREMENT,
  "title" varchar(200) NOT NULL
);

Reading ALTER TABLE

Adding a field shows an ALTER TABLE ... ADD COLUMN, the precise change Django will make to the existing table.

ALTER TABLE "blog_post"
  ADD COLUMN "published" bool NOT NULL DEFAULT 0;

Why Inspect First

Previewing SQL catches surprises early, like an unexpected rebuild or a costly change on a huge table.

Dialect Matters

The SQL reflects your configured database. Postgres, SQLite, and MySQL each produce slightly different statements.

Find the Number

Need the migration number? Run showmigrations first to list them, then pass the one you want to sqlmigrate.

python manage.py showmigrations blog

Backwards SQL

Add the --backwards flag to see the SQL Django would run to reverse a migration, useful before rolling back.

python manage.py sqlmigrate blog 0002 --backwards

A Learning Tool

sqlmigrate is a great way to learn how the ORM maps your models to tables, columns, indexes, and constraints.

Review Before Production

On important deploys, read the SQL for risky steps so a slow migration never locks your live tables unexpectedly.

Quick Check

What does python manage.py sqlmigrate do?

Recap

With sqlmigrate you preview the exact SQL behind any migration, forwards or backwards, so deploys hold no surprises. 🔍

Frequently asked questions

Is the “sqlmigrate and Inspecting SQL” lesson free?

Yes — the full text of “sqlmigrate and Inspecting SQL” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “sqlmigrate and Inspecting SQL”?

See the raw SQL a migration will run. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “sqlmigrate and Inspecting SQL” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. makemigrations vs migrate
  2. Reading a Migration File
  3. Changing a Model and Re-migrating
  4. sqlmigrate and Inspecting SQL
← Back to Django Academy