Django 1.7 and PSQL: on_delete=models.SET_NULL not work
<p>My models.py:</p>

<pre><code>class MyFile(models.Model):
    file = models.FileField(upload_to="myfiles", max_length=500, storage=OverwriteStorage())
    slug = models.SlugField(max_length=500, blank=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True,)
    date_created = models.DateTimeField(auto_now_add = True, blank=True, null=True)
    date_expired = models.DateTimeField(default=default_time, blank=True, null=True)
    expires = models.BooleanField(default=True)



    def __str__(self):
        return self.file.name

    @models.permalink
    def get_absolute_url(self):
        return ('myfile:myfile-new', )

    def save(self, *args, **kwargs):
        self.slug = self.file.name
        super(MyFile, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        self.file.delete(False)
        super(MyFile, self).delete(*args, **kwargs)

    class Meta:
        managed = True

@receiver(pre_delete, sender=MyFile)
def remove_file(**kwargs):
    instance = kwargs.get('instance')
    instance.file.delete(save=False)


class Operation(models.Model):
    id = models.AutoField(primary_key = True)
    user = models.ForeignKey(User, blank=True, null=True)
    myfile = models.ForeignKey(MyFile, blank=True, null=True)

    def get_data(self):
        return json.loads(self.data)

    class Meta:
        managed = True

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    subscribe = models.BooleanField(default=True)

    def __unicode__(self):
        return u'Profile of user: %s' % (self.user.username)

    class Meta:
        managed = True

def create_user_profile(sender, instance, created, **kwargs):
    from django.conf import settings
    subscribe = False 
    if created:
        profile, created = UserProfile.objects.get_or_create(user=instance, subscribe=subscribe)

post_save.connect(create_user_profile, sender=User)
</code></pre>

<p>But, if User deleted, all related myfiles still deleted.</p>

<p>Any idea? Thanks</p>

<p><strong>UPDATE</strong></p>

<p>Database is postgres9.3.5, and I used \d+ to check the information of tables before and after migration, <strong>NO difference</strong>. wired.</p>

<pre><code># \d+ ui.myproject_myfile;
                                                            Table "ui.myproject_myfile"
    Column    |           Type           |                            Modifiers                             | Storage  | Stats target | Description 
--------------+--------------------------+------------------------------------------------------------------+----------+--------------+-------------
 id           | integer                  | not null default nextval('ui.myproject_myfile_id_seq'::regclass) | plain    |              | 
 file         | character varying(500)   | not null                                                         | extended |              | 
 slug         | character varying(500)   | not null                                                         | extended |              | 
 user_id      | integer                  |                                                                  | plain    |              | 
 date_created | timestamp with time zone |                                                                  | plain    |              | 
 date_expired | timestamp with time zone |                                                                  | plain    |              | 
 expires      | boolean                  | not null                                                         | plain    |              | 
Indexes:
    "myproject_myfile_pkey" PRIMARY KEY, btree (id)
    "myproject_myfile_2dbcba41" btree (slug)
    "myproject_myfile_e8701ad4" btree (user_id)
Foreign-key constraints:
    "myproject_myfile_user_id_a422765c7101118_fk_auth_user_id" FOREIGN KEY (user_id) REFERENCES ui.auth_user(id) DEFERRABLE INITIALLY DEFERRED
Referenced by:
    TABLE "ui.myproject_operation" CONSTRAINT "myproject_ope_myfile_id_3f071e8e8361943b_fk_myproject_myfile_id" FOREIGN KEY (myfile_id) REFERENCES ui.myproject_myfile(id) DEFERRABLE INITIALLY DEFERRED
Has OIDs: no
</code></pre>

<p><strong>UPDATE</strong></p>

<pre><code>#ALTER TABLE ui.myproject_myfile ADD CONSTRAINT user_id_fk FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE SET NULL;
</code></pre>

<p><strong>Then, I found the following added to the descriptions of my table. But still not work. If user deleted, all myfiles deleted, too.</strong>
"user_id_fk" FOREIGN KEY (user_id) REFERENCES ui.auth_user(id) ON DELETE SET NULL</p>

<p><strong>UPDATE</strong></p>

<p>I am thinking about whether to delete this post. Problem solved after VM rebooted. NO idea why. But thank all of you.</p>

<p>The default behavior of <code>on_delete</code> results in all related objects being deleted; in this case, the related object is MyFile.</p>

<p>If you're trying to preserve the MyFile object, maybe consider adding a default via <code>on_delete=SET_DEFAULT</code> or using a callable passed to <code>on_default=SET(callable)</code></p>

python django django-models