﻿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
13940	The ManyToManyField raises IntegrityError when used with postgresql	DataGreed	nobody	"Update Django 1.2 has broken my site. 

Here's the affected model:

{{{
#!python 
class Category(models.Model):
    
    parent = models.ManyToManyField(""self"", symmetrical = False, null = True, blank = True)
    
    name = models.CharField(u""Name"",max_length = 500)
    
    alias = models.SlugField(u""Alias for URLs"")     
    
    creation_vars  = models.ManyToManyField(CatalogueVariables, symmetrical = True, blank = True, null=True,related_name = ""creation_vars"", verbose_name=u""New card vars"")
    
    filter_vars = models.ManyToManyField(CatalogueVariables, symmetrical = True, blank = True, null=True,verbose_name=u""Filter vars"")
    
    exp_filter_vars = models.ManyToManyField(CatalogueVariables, symmetrical = True, blank = True, null=True,related_name = ""exp_filter_vars"", verbose_name=u""Extended filter vars"")
   
    order = models.IntegerField(u""Ordering"", null = True, blank = True)
    
    class Meta():
        verbose_name = u""Category""
        verbose_name_plural = u""Categories""
}}}


Here's the related model:

{{{
#!python
class CatalogueVariables(models.Model):
           
    title = models.CharField(u""Name"", max_length = 100)    
    alias = models.SlugField(u""Alias max_length = 70)              
    status = models.PositiveIntegerField(u""Status"", choices = VariableStatusConstants.CHOICES, default = VariableStatusConstants.ENABLED)
}}}

Here is the problem:

{{{
#!python
>>> from catalogue.models import Category as c
>>> a  = c.objects.get(pk=362)
>>> from catalogue.models import CatalogueVariables as v
>>> s = v.objects.get(pk=452)
>>> a.creation_vars.add(s)
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py"", line 494, in add
    self._add_items(self.target_field_name, self.source_field_name, *objs)
  File ""/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py"", line 574, in _add_items
    '%s_id' % target_field_name: obj_id,
  File ""/usr/local/lib/python2.6/dist-packages/django/db/models/query.py"", line 352, in create
    obj.save(force_insert=True, using=self.db)
  File ""/usr/local/lib/python2.6/dist-packages/django/db/models/base.py"", line 435, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File ""/usr/local/lib/python2.6/dist-packages/django/db/models/base.py"", line 535, in save_base
    transaction.commit_unless_managed(using=using)
  File ""/usr/local/lib/python2.6/dist-packages/django/db/transaction.py"", line 175, in commit_unless_managed
    connection._commit()
  File ""/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py"", line 32, in _commit
    return self.connection.commit()
IntegrityError: insert or update on table ""catalogue_category_creation_vars"" violates foreign key constraint ""catalogue_category_creation_vars_category_id_fkey""
DETAIL:  Key (category_id)=(452) is not found in table ""catalogue_category"".
}}}

On django 1.0 everything worked well (and on 1.1, as i can remember)

The problem also exists in the admin site. But if I add !ManyToMany records manually, via pgadmin3 for postgresql - everything works well, no errors are returned and everything is ok."		closed	Database layer (models, ORM)	1.2		invalid	postgresql,		Unreviewed	0	0	0	0	0	0
