Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:31:29+00:00 2026-05-28T23:31:29+00:00

I have a Django 1.3 app for which I am using South 0.7.3 for

  • 0

I have a Django 1.3 app for which I am using South 0.7.3 for DB migrations. I have a problem where an on_delete=models.SET_NULL rule doesn’t seem to be firing when the parent entity is deleted, thus giving me a constraint violation from the underlying DB (which is Postgres 8.4).

The relevant parts of the entity defns are:

class AccessPeriod:
    ....

class Payment:
    period = models.ForeignKey( 
        AccessPeriod, related_name = "payments", db_index = True,
        null = True, on_delete = models.SET_NULL )

After some digging, I discovered that South doesn’t actually insert the ON DELETE clause into the SQL it generates for migrations, so the DB is definitely not going to do the nullifying on the broken relationships itself:

http://south.aeracode.org/ticket/763

Then I read the Django docs for on_delete rules, which state (my emphasis):

When an object referenced by a ForeignKey is deleted, Django by
default emulates the behavior of the SQL constraint ON DELETE CASCADE
and also deletes the object containing the ForeignKey. This behavior
can be overridden by specifying the on_delete argument.

The “emulates” part suggested to me that Django tries to implement the on_delete behaviour itself and does not rely on the underlying DB to automatically execute this as part of the transaction, thus making the South bug irrelevant.

I had a poke through db/models/deletion.py in the Django source and based on the implementation of SET() / SET_NULL() and collect() it certainly seems like Django is supposed to do this itself, however, if I try to delete an AccessPeriod from the Django admin, I get constraint violations on the Payments table for the ID it still references which is now deleted, i.e. it doesn’t seem like Django is calling SET_NULL() on the Payment.period relationship as part of the call to accessPeriod.delete().

Am I doing something wrong here, or misunderstanding what Django should be doing? Just trying to avoid manually hacking the DB to insert the ON DELETE rule myself, which feels extremely brittle and horrible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:31:29+00:00Added an answer on May 28, 2026 at 11:31 pm

    In my specific case, I traced this bug down to declarations of ModelAdmin inline in my models.py code causing my model classes to be instantiated incorrectly, with broken on_delete behaviour the most visible side-effect of this. From my commit message:

    Fixed issue with cascading deletes not being done properly in Django
    DB.

    Turns out it’s really important to not declare your ModelAdmins at the global scope in models.py, otherwise all the relationships
    between the different models get calculated before all the models are
    loaded and lots of them get left out. Really isn’t that emphatic
    about this in the Django documentation.

    So in my original (broken) code, I would have the ModelAdmin for each Model declared immediately after it, a la:

    class AccessPeriod( models.Model ):
        ....
    
    class AccessPeriodAdmin( models.ModelAdmin ):
        ....
    
    # This causes the metaclass setup for AccessPeriod to happen right now,
    # and since related models for AccessPeriod are not all declared yet,
    # relationship handling behaviour becomes broken for AccessPeriod
    admin.site.register( AccessPeriod, AccessPeriodAdmin )
    
    class Payment( models.Model ):
        ....
    
    class PaymentAdmin( models.ModelAdmin ):
        ....
    
    # Same effect on the Payment model here    
    admin.site.register( Payment, PaymentAdmin )
    

    The solution was to move all the ModelAdmin declarations out of myapp/models.py and into myapp/admin.py so that all the metaclass setup for each class happens after all the model declarations have been handled, and then all the relationships started behaving properly again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i am using django 1.2 .I my case i have few models which
I have a django app which is used for managing registrations to a survey.
I have a Django app which uses LDAP as the authentication backend. I'm not
In my django app, I have a view which accomplishes file upload.The core snippet
I have a django app with models as follows: A Question model An Answer
So, I have created a django app here: http://fpaste.org/bkFf/ without using django-admin. I want
I am working on a web-app using Django 1.3 and Python2.6. I have to
I have a django project with 2 apps like this: ## tags app, models.py
Am using django forms on my app. I have a model Client like this:
I'm using django-ajax-selects , which is a freely available django app providing jquery autocomplete

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.