sqlmigrate e Inspeção de SQL
Veja o SQL bruto que uma migração executará
sqlmigrate e Inspeção de SQL é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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 0002It 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 blogBackwards 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 --backwardsA 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. 🔍
Perguntas Frequentes
A aula “sqlmigrate e Inspeção de SQL” é grátis?
Sim — o texto completo de “sqlmigrate e Inspeção de SQL” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “sqlmigrate e Inspeção de SQL”?
Veja o SQL bruto que uma migração executará Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “sqlmigrate e Inspeção de SQL”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- makemigrations vs migrate
- Lendo um Arquivo de Migração
- Alterando um Modelo e Migrando Novamente
- sqlmigrate e Inspeção de SQL