Opened 8 years ago

Closed 3 years ago

#9371 closed Bug (wontfix)

Fixtures combined with inherited models causing constraint violations/possible double entry of data

Reported by: terpsquared Owned by: nobody
Component: Core (Serialization) Version: 1.0
Severity: Normal Keywords:
Cc: djsnickles@…, sorin, andre.miras+djangoproject@…, carlos.palol@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Attachments (1)

testproj.tar.gz (2.9 KB) - added by terpsquared 8 years ago.

Download all attachments as: .zip

Change History (13)

Changed 8 years ago by terpsquared

Attachment: testproj.tar.gz added

comment:1 Changed 8 years ago by Russell Keith-Magee

I have a feeling that this could be related to #8886 - the fact that your child model duplicates a field name in the parent model (username). I'll need to look a little closer to be certain.

comment:2 Changed 8 years ago by Jacob

Triage Stage: UnreviewedAccepted

comment:3 Changed 6 years ago by Eric Holscher

Component: UncategorizedSerialization

comment:4 Changed 6 years ago by tehfink <djsnickles@…>

Cc: djsnickles@… added
Easy pickings: unset
Severity: Normal
Type: Uncategorized

comment:5 Changed 6 years ago by Luke Plant

Type: UncategorizedBug

comment:6 Changed 6 years ago by sorin

Cc: sorin added

comment:7 Changed 5 years ago by anonymous

Easy pickings: set
Has patch: set
milestone: 2.0
Needs documentation: set
Needs tests: set
Patch needs improvement: set
Resolution: invalid
Status: newclosed
UI/UX: set

comment:8 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset
Has patch: unset
milestone: 2.0
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: invalid
Status: closedreopened
UI/UX: unset

Revert "spam".

comment:9 Changed 5 years ago by Andre Miras

Cc: andre.miras+djangoproject@… added

comment:10 Changed 5 years ago by carlos.palol@…

Cc: carlos.palol@… added

comment:11 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:12 Changed 3 years ago by Aymeric Augustin

Resolution: wontfix
Status: newclosed

The new migrations framework doesn't load initial data any more.

Note: See TracTickets for help on using tickets.
Back to Top