Scott Deerwester
Scott Deerwester

Reputation: 3977

In Django, is on_delete implemented in Python, or using DB triggers, for PostgreSQL databases?

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?

  1. The dependent record will be deleted by PostgreSQL, or
  2. the dependent record should be deleted "manually" by the microserver (this is what I currently do),

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

Answers (1)

AdamKG
AdamKG

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

Related Questions