0Pricing
Flask Academy · درس

تهيئة عملية ترحيل وإنشاؤها تلقائيًا

شغّل flask db init وmigrate.

تهيئة عملية ترحيل وإنشاؤها تلقائيًا درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🎉

الأسئلة الشائعة

هل درس «تهيئة عملية ترحيل وإنشاؤها تلقائيًا» مجاني؟

نعم — نص درس «تهيئة عملية ترحيل وإنشاؤها تلقائيًا» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «تهيئة عملية ترحيل وإنشاؤها تلقائيًا»؟

شغّل flask db init وmigrate. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «تهيئة عملية ترحيل وإنشاؤها تلقائيًا»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا تحتاج إلى عمليات الترحيل بدلًا من create_all
  2. تهيئة عملية ترحيل وإنشاؤها تلقائيًا
  3. تطبيق الترحيل والتراجع عنه باستخدام upgrade
  4. مراجعة البرامج النصية المُنشأة وتحريرها
← العودة إلى Flask Academy