Challenge - 5 Problems
On_delete Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a parent object is deleted with CASCADE?
Consider a Django model where a child model has a ForeignKey to a parent model with on_delete=models.CASCADE. What is the behavior when the parent object is deleted?
Django
class Parent(models.Model): name = models.CharField(max_length=100) class Child(models.Model): parent = models.ForeignKey(Parent, on_delete=models.CASCADE) name = models.CharField(max_length=100)
Attempts:
2 left
💡 Hint
Think about what CASCADE means in everyday life, like a waterfall effect.
✗ Incorrect
CASCADE means when the parent is deleted, all related child objects are also deleted automatically. This prevents orphaned child records.
❓ component_behavior
intermediate2:00remaining
What error occurs with PROTECT on deleting a parent?
If a ForeignKey uses on_delete=models.PROTECT, what happens when you try to delete the parent object that has child objects linked to it?
Django
class Parent(models.Model): name = models.CharField(max_length=100) class Child(models.Model): parent = models.ForeignKey(Parent, on_delete=models.PROTECT) name = models.CharField(max_length=100)
Attempts:
2 left
💡 Hint
PROTECT means to stop something from happening.
✗ Incorrect
PROTECT prevents deletion of the parent if any child objects exist. Django raises a ProtectedError to block the deletion.
❓ component_behavior
advanced2:00remaining
What is the effect of SET_NULL on deleting a parent object?
Given a ForeignKey with on_delete=models.SET_NULL and null=True, what happens to child objects when the parent is deleted?
Django
class Parent(models.Model): name = models.CharField(max_length=100) class Child(models.Model): parent = models.ForeignKey(Parent, on_delete=models.SET_NULL, null=True) name = models.CharField(max_length=100)
Attempts:
2 left
💡 Hint
Think about removing a link but keeping the child safe.
✗ Incorrect
SET_NULL sets the foreign key in child objects to NULL when the parent is deleted, so children remain but without a parent link.
📝 Syntax
advanced2:00remaining
Which ForeignKey declaration is correct for SET_NULL?
You want to use on_delete=models.SET_NULL in a ForeignKey. Which option is syntactically correct and will work without errors?
Attempts:
2 left
💡 Hint
SET_NULL requires the field to accept NULL values.
✗ Incorrect
Using SET_NULL requires null=True so the database allows NULL values. Without it, a database error occurs.
❓ state_output
expert3:00remaining
What is the count of Child objects after deleting a Parent with mixed on_delete behaviors?
Given these models and data, what is the number of Child objects remaining after deleting the Parent with id=1?
Django
class Parent(models.Model): name = models.CharField(max_length=100) class ChildCascade(models.Model): parent = models.ForeignKey(Parent, on_delete=models.CASCADE) class ChildProtect(models.Model): parent = models.ForeignKey(Parent, on_delete=models.PROTECT) class ChildSetNull(models.Model): parent = models.ForeignKey(Parent, on_delete=models.SET_NULL, null=True) # Setup: # Parent object with id=1 exists # ChildCascade objects linked to Parent 1: 3 # ChildProtect objects linked to Parent 1: 2 # ChildSetNull objects linked to Parent 1: 4 # Action: # Attempt to delete Parent with id=1
Attempts:
2 left
💡 Hint
PROTECT blocks deletion if any child exists.
✗ Incorrect
Because ChildProtect uses PROTECT and there are 2 such children, deleting the parent raises ProtectedError and no deletion happens. All children remain.