ตรวจทานและแก้สคริปต์ที่สร้างขึ้น
แก้การย้ายแบบแผนที่สร้างอัตโนมัติก่อนนำไปใช้
ตรวจทานและแก้สคริปต์ที่สร้างขึ้น เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Autogenerate Is Not Perfect
Autogenerate is a great first draft, not a final answer. Always review the script before you apply it anywhere. 🔍
Open the Versions File
Each script lives in the versions folder. Open it and read the upgrade and downgrade functions like any other code.
The op Object
Inside the functions you will see calls on the op object, like op.create_table and op.add_column, each describing one schema change.
op.add_column("user", sa.Column("age", sa.Integer()))What Alembic Misses
Autogenerate often misses renames, seeing them as a drop plus an add. That would silently delete the column data.
Fix Renames by Hand
When a column is really being renamed, replace the drop and add with a single alter_column that preserves the data.
op.alter_column("user", "name", new_column_name="full_name")Check the downgrade
Confirm the downgrade truly reverses the upgrade. A broken downgrade leaves you stuck if you ever need to roll back.
Watch NOT NULL Columns
Adding a non-nullable column to a table with rows will fail. Add it nullable first, fill values, then make it required.
Add Data Steps Yourself
Need to backfill values? Add an op.execute call with raw SQL between the schema steps to migrate existing data safely.
op.execute("UPDATE user SET age = 0")Test on a Copy First
Before touching production, run the migration on a copy of real data. It surfaces failures while they are still cheap to fix.
One Change per Migration
Keep each migration focused on one logical change. Small scripts are far easier to review, test, and roll back cleanly.
Commit the Final Script
Once edited and tested, commit the script. Never change a migration that has already shipped to production history.
Quick Check
You renamed a column, but autogenerate wrote a drop and an add. What is the danger?
Recap
You learned to review generated scripts, fix renames with alter_column, handle non-null columns, and test before shipping. 🎉
คำถามที่พบบ่อย
บทเรียน “ตรวจทานและแก้สคริปต์ที่สร้างขึ้น” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตรวจทานและแก้สคริปต์ที่สร้างขึ้น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตรวจทานและแก้สคริปต์ที่สร้างขึ้น”
แก้การย้ายแบบแผนที่สร้างอัตโนมัติก่อนนำไปใช้ คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ตรวจทานและแก้สคริปต์ที่สร้างขึ้น” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดจึงต้องใช้การย้ายแบบแผนแทน create_all
- เริ่มต้นและสร้างการย้ายแบบแผนอัตโนมัติ
- ใช้และย้อนกลับด้วย upgrade
- ตรวจทานและแก้สคริปต์ที่สร้างขึ้น