sqlmigrateでSQLを調べる
マイグレーションが実行する生のSQLを確認します
「sqlmigrateでSQLを調べる」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🔍
よくある質問
「sqlmigrateでSQLを調べる」レッスンは無料ですか?
はい。「sqlmigrateでSQLを調べる」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「sqlmigrateでSQLを調べる」で何を学びますか?
マイグレーションが実行する生のSQLを確認します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「sqlmigrateでSQLを調べる」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- makemigrationsとmigrateの違い
- マイグレーションファイルを読む
- モデルを変更して再度マイグレーションする
- sqlmigrateでSQLを調べる