#9371: Fixtures combined with inherited models causing constraint
violations/possible double entry of data
---------------------------+------------------------------------------------
 Reporter:  terpsquared    |       Owner:  nobody    
   Status:  new            |   Milestone:            
Component:  Uncategorized  |     Version:  1.0       
 Keywords:                 |       Stage:  Unreviewed
Has_patch:  0              |  
---------------------------+------------------------------------------------
 Using a model descended from the provided User model (as described in
 http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-
 with-inheritance/), in combination with using a fixture, seems to attempt
 to load the same data twice, resulting in the following:


 {{{
 $ python ./manage.py syncdb
 Creating table auth_permission
 Creating table auth_group
 Creating table auth_user
 Creating table auth_message
 Creating table django_content_type
 Creating table django_session
 Creating table django_admin_log
 Creating table testapp_customuser

 You just installed Django's auth system, which means you don't have any
 superusers defined.
 Would you like to create one now? (yes/no): no
 Installing index for auth.Permission model
 Installing index for auth.Message model
 Installing index for admin.LogEntry model
 Installing json fixture 'initial_data' from absolute path.
 Problem installing fixture 'initial_data.json': Traceback (most recent
 call last):
   File "/usr/lib/python2.4/site-
 packages/django/core/management/commands/loaddata.py", line 116, in handle
     obj.save()
   File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py",
 line 163, in save
     models.Model.save_base(self.object, raw=True)
   File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line
 379, in save_base
     result = manager._insert(values, return_id=update_pk)
   File "/usr/lib/python2.4/site-packages/django/db/models/manager.py",
 line 138, in _insert
     return insert_query(self.model, values, **kwargs)
   File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line
 894, in insert_query
     return query.execute_sql(return_id)
   File "/usr/lib/python2.4/site-
 packages/django/db/models/sql/subqueries.py", line 309, in execute_sql
     cursor = super(InsertQuery, self).execute_sql(None)
   File "/usr/lib/python2.4/site-packages/django/db/models/sql/query.py",
 line 1724, in execute_sql
     cursor.execute(sql, params)
   File "/usr/lib/python2.4/site-packages/django/db/backends/util.py", line
 19, in execute
     return self.cursor.execute(sql, params)
   File "/usr/lib/python2.4/site-
 packages/django/db/backends/sqlite3/base.py", line 168, in execute
     return Database.Cursor.execute(self, query, params)
 IntegrityError: column username is not unique

 }}}

 Same thing with a postgres db:

 {{{
 python ./manage.py syncdb
 Installing json fixture 'initial_data' from absolute path.
 Problem installing fixture 'initial_data.json': Traceback (most recent
 call last):
   File "/usr/lib/python2.4/site-
 packages/django/core/management/commands/loaddata.py", line 116, in handle
     obj.save()
   File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py",
 line 163, in save
     models.Model.save_base(self.object, raw=True)
   File "/usr/lib/python2.4/site-packages/django/db/models/base.py", line
 379, in save_base
     result = manager._insert(values, return_id=update_pk)
   File "/usr/lib/python2.4/site-packages/django/db/models/manager.py",
 line 138, in _insert
     return insert_query(self.model, values, **kwargs)
   File "/usr/lib/python2.4/site-packages/django/db/models/query.py", line
 894, in insert_query
     return query.execute_sql(return_id)
   File "/usr/lib/python2.4/site-
 packages/django/db/models/sql/subqueries.py", line 309, in execute_sql
     cursor = super(InsertQuery, self).execute_sql(None)
   File "/usr/lib/python2.4/site-packages/django/db/models/sql/query.py",
 line 1724, in execute_sql
     cursor.execute(sql, params)
   File "/usr/lib/python2.4/site-packages/django/db/backends/util.py", line
 19, in execute
     return self.cursor.execute(sql, params)
 IntegrityError: duplicate key value violates unique constraint
 "testapp_customuser_username_key"

 }}}

 Note: This does not happen when only one record exists in the fixture
 data.  The sample data I provide has two unique records that should not
 clash.

 The following is the model definition that seems to not be handled:

 {{{
 from django.db import models
 from django.contrib.auth.models import User, UserManager
 from django.utils.translation import ugettext_lazy as _
 from datetime import datetime


 class Customuser(User):
     username = models.CharField(_('customer id'), max_length=30,
 unique=True, help_text=_("Required. 30 characters or fewer. Alphanumeric
 characters only (letters, digits and underscores)."))
     email_id = models.EmailField(_('e-mail address'), blank=True,
 null=True, unique=True, db_index=True)
     name = models.CharField(max_length=100, blank=True)
     address_1 = models.CharField(max_length=255, blank=True)
     address_2 = models.CharField(max_length=255, blank=True)
     city = models.CharField(max_length=80, blank=True)
     state = models.CharField(max_length=13, blank=True)
     zipcode = models.CharField(max_length=13, blank=True)
     home_phone = models.CharField(max_length=15, blank=True)
     mobile_phone = models.CharField(max_length=15, blank=True)

     CONTACT_PREFERANCE_CHOICES = (
         ('E', 'E-Mail'),
         ('M', 'Regular Mail'),
         ('P', 'Phone'),
         ('N', 'No contact'),
     )

     contact_method = models.CharField(max_length=1,
 choices=CONTACT_PREFERANCE_CHOICES, blank=True)
     member_since = models.DateField(blank=True, null=True)
     last_activity = models.DateField(blank=True, null=True)
     point_balance = models.IntegerField(default=0)
     NHRA_member = models.BooleanField(default=False)
     IRL_member = models.BooleanField(default=False)

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

     # Use UserManager to get the create_user method, etc.
     objects = UserManager()
 }}}

 I have provided a sample app that will reproduce this issue.  Both 1.0 and
 the latest SVN have this issue.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9371>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to