0Pricing
Flask Academy · درس

تعريف العلاقات والمفاتيح الخارجية

اربط النماذج بعلاقات واحد إلى متعدد.

تعريف العلاقات والمفاتيح الخارجية درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Rows That Belong Together

Real data connects: a user has many posts, an order has many items. A relationship links these rows in your models. 🔗

The Foreign Key

A foreign key is a column on the child that stores the parent's id. It is the actual link saved in the database.

user_id = db.Column(db.Integer, db.ForeignKey("user.id"))

Point at the Right Table

Inside ForeignKey you name the target as table.column, using the lowercase table name Flask-SQLAlchemy generates.

db.ForeignKey("user.id")

Add a relationship

The relationship() helper turns that key into easy navigation, so you reach linked objects as attributes.

posts = db.relationship("Post")

One-to-Many in Practice

Put the foreign key on the many side and the relationship on the one side. One user, many posts.

class Post(db.Model):
    user_id = db.Column(db.ForeignKey("user.id"))

Navigate Parent to Children

Once linked, read a parent's children like a list. SQLAlchemy fetches them for you on access. ✨

user = User.query.first()
for p in user.posts:
    print(p.title)

Walk Back with backref

Add backref to a relationship and the child instantly gains a way to reach its parent.

posts = db.relationship("Post", backref="author")

Use the Backref

That backref lets you go from a child straight back to its owner with a clean attribute. 🙌

post = Post.query.first()
print(post.author.name)

Assign by Object

Link rows by setting the relationship attribute to an object. SQLAlchemy fills in the foreign key for you.

post = Post(title="Hi")
post.author = user

Cascade on Delete

Set a cascade so deleting a parent can also remove its children, keeping the database tidy and consistent.

db.relationship("Post", cascade="all, delete")

Key vs Relationship

The foreign key stores the link in SQL; relationship is the Python convenience layer on top of it. You usually want both.

Quick Check

One user can write many posts. Where does the foreign key column live?

Recap: Linking Models

You now connect tables with a foreign key, navigate them via relationship and backref, and control deletes with cascade. Models, united! 🎉

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

هل درس «تعريف العلاقات والمفاتيح الخارجية» مجاني؟

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

ماذا ستتعلم في «تعريف العلاقات والمفاتيح الخارجية»؟

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

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

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

كم من الوقت يستغرق درس «تعريف العلاقات والمفاتيح الخارجية»؟

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

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

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

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

  1. جلب الصفوف باستخدام query وget
  2. تصفية النتائج وترتيبها وتحديد عددها
  3. تعريف العلاقات والمفاتيح الخارجية
  4. عمليات الربط والتحميل الكسول مقابل المتعجل
← العودة إلى Flask Academy