Reputation: 3977
I have an application that shares a database between a large Django application, and a set of microservers (written in Go). If a record is deleted by a microserver, such that a record in a different table has a foreign key that references it, and if I've specified on_delete=models.CASCADE
in the respective ForeignKey
field, which of the following is true?
or is there a way to specify how I want on_delete
to be handled?
Here's a bit of an example for completeness:
from django.contrib.gis.db import models
class Parent(models.Model):
name = models.CharField(max_length=128)
class Child(models.Model):
name = models.CharField(max_length=128)
parent = models.ForeignKey(
Parent,
null=False,
on_delete=models.CASCADE)
and in Go:
_, err = db.Exec(`DELETE FROM module_parent WHERE ID = $1`, parentID)
or do I have to do:
_, err = db.EXEC(`DELETE FROM module_child WHERE parent_id = $1`, parentID)
...
_ err = db.EXEC(`DELETE FROM module_parent WHERE id = $1`, parentID
Upvotes: 0
Views: 137
Reputation: 14066
The 2nd is true, per the Django docs for ForeignKey:
on_delete
doesn’t create a SQL constraint in the database. Support for database-level cascade options may be implemented later.
Upvotes: 1