حذف الصفوف المفقودة ومعالجتها
احذف السجلات وأعِد 404 عند غيابها.
حذف الصفوف المفقودة ومعالجتها درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The D in CRUD
Deleting removes a row for good. Like updating, you first load the record, then tell the session to drop it. 🗑️
Load the Target Row
Fetch the record by id before deleting. With get_or_404() a missing id stops the request cleanly with a 404.
user = User.query.get_or_404(user_id)Mark It for Deletion
Stage the removal with db.session.delete(). The row is not gone yet, just queued in the current transaction.
db.session.delete(user)Commit to Remove
As with every write, nothing happens until you commit(). That executes the DELETE and the row is gone for real.
db.session.delete(user)
db.session.commit()Handle Missing Rows First
Deleting an id that does not exist is a client error. Reply with 404 rather than pretending it worked.
user = User.query.get(user_id)
if user is None:
return jsonify(error="not found"), 404204 No Content
A clean delete often returns 204, the status that means success with nothing useful left to send back.
return "", 204Roll Back on Error
If the commit fails, perhaps a foreign key blocks it, call rollback() to undo the staged delete and keep data consistent.
except Exception:
db.session.rollback()Delete Is Idempotent
Calling delete twice on the same id should behave the same: the row is simply absent, so a repeat 404 is fine.
Watch the Relationships
Deleting a parent can orphan children. Configure cascade on the relationship so related rows are cleaned up too.
posts = relationship("Post", cascade="all, delete")Consider Soft Deletes
Sometimes you should not erase data. A soft delete flips an is_deleted flag instead, keeping the row for audits.
user.is_deleted = True
db.session.commit()Load, Delete, Commit
The delete flow mirrors update: load the row, mark it for deletion, then commit. Missing ids get a tidy 404.
Quick Check
A DELETE request succeeds and there is no body to return. Which status fits best?
Recap: Deleting Records
You load with get_or_404, call delete, commit, and answer 204, with cascades and soft deletes as smart options. CRUD complete! 🎉
الأسئلة الشائعة
هل درس «حذف الصفوف المفقودة ومعالجتها» مجاني؟
نعم — نص درس «حذف الصفوف المفقودة ومعالجتها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «حذف الصفوف المفقودة ومعالجتها»؟
احذف السجلات وأعِد 404 عند غيابها. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «حذف الصفوف المفقودة ومعالجتها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إنشاء السجلات وتثبيت الجلسات
- نقاط نهاية القراءة المفردة والمتعددة
- تحديث السجلات الموجودة بأمان
- حذف الصفوف المفقودة ومعالجتها