0Pricing
Flask Academy · บทเรียน

เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ

เรียกใช้ flask db init และ migrate

เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Install Flask-Migrate

First add the package to your project. Flask-Migrate brings Alembic and a set of handy flask db commands. 📦

pip install Flask-Migrate

Wire It to the App

Create a Migrate object and bind it to your app and db. That single line activates the whole flask db command group.

from flask_migrate import Migrate
migrate = Migrate(app, db)

Initialize the Repo

Run flask db init once per project. It scaffolds a migrations folder that will hold every version script you generate.

flask db init

Inside the Folder

The new folder holds an alembic.ini, an env.py, and an empty versions directory where future migration files will live.

Autogenerate a Migration

flask db migrate compares your models to the live schema and writes a script describing exactly what changed.

flask db migrate -m "create users table"

The -m Message

Always pass a clear -m message. It labels the migration so your team can read the history like a changelog later.

A New Versions File

Each migrate command drops a fresh file in versions, named with a random revision id plus your message slug.

Models Must Be Imported

Autogenerate only sees models that are actually imported when the command runs. Missing imports mean missing tables in the script.

It Detects Differences

Alembic diffs the model metadata against the database. It catches new tables, added columns, and many changed constraints automatically.

Migrate Does Not Apply

Key point: migrate only writes the script. Your database is still untouched until you actually apply it in the next lesson.

Commit the Migration

Treat migration files as code and commit them to git. Teammates replay the exact same changes from your version history.

Quick Check

You ran flask db migrate and saw a new file appear. Did your tables change?

Recap

You wired up Migrate, ran flask db init, and autogenerated a script with flask db migrate ready to apply. 🎉

คำถามที่พบบ่อย

บทเรียน “เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ”

เรียกใช้ flask db init และ migrate คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใดจึงต้องใช้การย้ายแบบแผนแทน create_all
  2. เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ
  3. ใช้และย้อนกลับด้วย upgrade
  4. ตรวจทานและแก้สคริปต์ที่สร้างขึ้น
← กลับไปที่ Flask Academy