﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14896	Delete leads to IntegrityError : bad cascading rule when there's a ManyToManyField pointing to a class having subclasses.	tbrizzi	Carl Meyer	"I'm working on a scientific Django based application. One of its features is to store all researchers of a laboratory and relative contacts like addresses, phones, faxes, websites or emails :

{{{
class Contact(Cast):
    """"""Base class of all type of contacts like Address, Phone, Fax, E-mail or Website.""""""
    label = models.CharField(max_length=100)
    
    def __unicode__(self):
        return self.label
    
    class Meta :
        ordering = ['label']

class EMail(Contact):
    """"""A subclass of Contact to store an EMail address.""""""
    identifier = models.EmailField(verbose_name=""address"", max_length=256)
    
    def __unicode__(self):
        st = ""%s : %s"" % (self.label, self.identifier)
        return st
    
    class Meta:
        verbose_name = ""e-mail""

class Researcher(models.Model):
    """"""Anybody working in a ScientificStructure.""""""
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    photo = models.ImageField(upload_to=""uploads/images/photos"", null=True, blank=True)
    db_user = models.OneToOneField(User, null=True, blank=True, verbose_name=""database user"")
    contacts = models.ManyToManyField(Contact, null=True, blank=True)
    notes = models.TextField(null=True, blank=True)
    
    def get_full_name(self):
        """"""Get the full name of a Researcher, i.e. the combination between its first and last names.""""""
        return ""%s %s"" % (self.first_name, self.last_name)
    
    def __unicode__(self):
        return self.get_full_name()
    
    class Meta:
        ordering = ['last_name', 'first_name']
}}}

From IPython, I create and link together a Researcher and an EMail address :

{{{

In [1]: from helmholtz.people.models import EMail, Researcher

In [2]: researcher = Researcher.objects.create(first_name='Django', last_name='Reinhardt')

In [3]: email = EMail.objects.create(label='django reinhardt email', identifier='django.reinhardt@gmail.com')

In [4]: researcher.contacts.add(email)

}}}

When I execute the following piece of code from IPython, the interpreter returns this :

{{{


In [5]: email.delete()
---------------------------------------------------------------------------
IntegrityError                            Traceback (most recent call last)

C:\Users\Thierry.NeuroInf-DB_2\<ipython console> in <module>()

D:\Thierry\Projects\django-svn\django\db\models\base.pyc in delete(self, using)
    577         collector = Collector(using=using)
    578         collector.collect([self])
--> 579         collector.delete()
    580
    581     delete.alters_data = True

D:\Thierry\Projects\django-svn\django\db\models\deletion.pyc in decorated(self, *args, **kwargs)
     48             func(self, *args, **kwargs)
     49             if forced_managed:
---> 50                 transaction.commit(using=self.using)
     51             else:
     52                 transaction.commit_unless_managed(using=self.using)

D:\Thierry\Projects\django-svn\django\db\transaction.pyc in commit(using)
    199         using = DEFAULT_DB_ALIAS
    200     connection = connections[using]
--> 201     connection._commit()
    202     set_clean(using=using)
    203

D:\Thierry\Projects\django-svn\django\db\backends\postgresql_psycopg2\base.pyc in _commit(self)
    198         if self.connection is not None:
    199             try:
--> 200                 return self.connection.commit()
    201             except Database.IntegrityError, e:
    202                 raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]

IntegrityError: ERROR:  UPDATE or DELETE on table ""people_contact"" violates foreign key constraint
""people_researcher_contacts_contact_id_fkey"" on table ""people_researcher_contacts""
DETAIL:  key (id)=(5) is still referenced from table ""people_researcher_contacts"".

}}}

Seems there's a bad cascading rule when there's a many to many field pointing to a class having subclasses. 

This error has never happened before Django 1.3 alpha."		closed	Database layer (models, ORM)	1.3-alpha		fixed	blocker	Alexander Koshelev	Accepted	0	0	0	0	0	0
