Skip to main content

Fork of django-db-cascade for Django 2.0. Optionally use postgres db ON CASCADE DELETE on django foreign keys

Project description

django-db-cascade-2

Installation for Django 3

pip install django-db-cascade-2

Installation for Django 2

pip install django-db-cascade-2==0.2.3

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django_db_cascade.backends.postgresql_psycopg2',
        # ... etc ...
    }
}

Usage

from django.db import models
from django_db_cascade.fields import ForeignKey, OneToOneField
from django_db_cascade.deletions import DB_CASCADE

class Thing(Common):
    account = ForeignKey('self', DB_CASCADE)

Caveats

  • DB_CASCADE only supports Postgres
  • DB_CASCADE does not support django on_delete signals
  • DB_CASCADE will not cascade delete multiple inherited tables as expected
  • DB_CASCADE will not trigger CASCADE on another model. E.g. Model A points to model B, via DB_CASCADE. Model B points to model C, via CASCADE. A will cascade delete B, B will django delete C, but deleting A will not delete C!
  • DB_CASCADE on a ManyToMany of A <---> B, only A_B set records will be cascade deleted (deleting A will not delete B)

How it works

  1. Minimal subclassing of the django postgresql backend and the django ForeignKey field
  2. Added a new possible value for ForeignKey's on_delete kwarg, called DB_CASCADE
  3. When you use DB_CASCADE, the migration framework will recognize a change, and write new sql
  4. example SQL generated:
    ALTER TABLE mytable ADD CONSTRAINT myconstraint FOREIGN KEY (mycolumn)
    REFERENCES myothertable myothercolumn ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
    

Future proof

If, and when, DB_CASCADE ever gets into django, editing these generated migrations should be very easy.

Generated migrations:

migrations.AlterField(
    model_name='modelname',
    name='fieldname',
    field=django_db_cascade.fields.ForeignKey(on_delete=django_db_cascade.deletions.DB_CASCADE)
)

Changing them over, if django ever handles DB_CASCADE natively, might look like:

migrations.AlterField(
    model_name='modelname',
    name='fieldname',
    field=django.db.models.ForeignKey(on_delete=models.DB_CASCADE)
)

Ticket

The ticker where django has discussed bringing DB_CASCADE to django: https://code.djangoproject.com/ticket/21961

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-db-cascade-2-0.3.4.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

django_db_cascade_2-0.3.4-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django-db-cascade-2-0.3.4.tar.gz.

File metadata

  • Download URL: django-db-cascade-2-0.3.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for django-db-cascade-2-0.3.4.tar.gz
Algorithm Hash digest
SHA256 d216ec2dac97b225b55204b053d0163c2c058f78f0a9932ec6ae20ec3c376314
MD5 18cca9224318ee8d43573fcafd7f50ad
BLAKE2b-256 4a2d99dde3c19778121d2e5f2e682c54225d7490b43e4a42b7c8351b0f4224f7

See more details on using hashes here.

File details

Details for the file django_db_cascade_2-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_db_cascade_2-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e9aa5810b4ba45f4b1e4cdb99a836236c4e2d0d218a3569145cacb8f8464914e
MD5 febdf8cad500bba5b102139118eeb470
BLAKE2b-256 ad87d9fc286d5cf818bf9aa0e69229ad6478df32b744d6211410ef8e1d491fe8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page