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

เหตุใดจึงต้องใช้การย้ายแบบแผนแทน create_all

ความเสี่ยงของการแก้แบบแผนที่ใช้งานจริงด้วยมือ

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

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

Schemas Always Change

Real apps evolve, so your tables must change too. You will add columns, rename fields, and drop old ones as features grow. 🔄

create_all Only Builds Once

Remember the limit you met earlier: create_all only makes missing tables. It never touches a table that already exists.

db.create_all()

The Tempting Bad Fix

One tempting fix is drop then recreate. It works in a demo, but it deletes every row, which is unthinkable in production.

db.drop_all()
db.create_all()

Editing by Hand Is Risky

Running raw ALTER TABLE by hand on a live database is fragile. One typo or missed step can corrupt data with no easy undo.

What a Migration Is

A migration is a small script that moves your schema from one version to the next in safe, repeatable steps.

Versioned and Ordered

Each migration has a version id and points to the one before it. Together they form an ordered history of every schema change.

Forward and Backward

A migration defines two paths: upgrade applies the change, and downgrade reverses it if something goes wrong.

def upgrade():
    ...

def downgrade():
    ...

Flask-Migrate Wraps Alembic

Flask-Migrate connects your app to Alembic, the tool that detects schema changes and writes the migration scripts for you.

It Tracks the Current Version

Migrations record the current schema version inside a table called alembic_version, so the tool always knows where you are.

Safe Across Every Environment

The same migration scripts run on your laptop, in CI, and in production, so every environment ends up with an identical schema.

When to Switch

Use create_all only for the very first prototype. The moment your model starts changing, move to migrations for good.

Quick Check

Your table exists and you just added a column. Why not rely on create_all?

Recap

You saw why create_all cannot evolve a schema and how migrations bring safe, versioned, reversible changes. 🎉

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

บทเรียน “เหตุใดจึงต้องใช้การย้ายแบบแผนแทน create_all” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “เหตุใดจึงต้องใช้การย้ายแบบแผนแทน create_all”

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

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

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

บทเรียน “เหตุใดจึงต้องใช้การย้ายแบบแผนแทน create_all” ใช้เวลานานแค่ไหน

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

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

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

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

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