﻿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
3790	New model object's pk clashes with fixture data	scott@…	Russell Keith-Magee	"I'm using fixtures with django.test.!TestCase.  If my test creates a model object and the fixture data already contains an object of the same type with pk=1, the new object will fail to save because of a pk clash.  If my fixture data has pk=2, my created object is saved fine (with pk=1), but a subsequent object will fail with a pk clash (trying to use pk=2, already taken).

It's as if the sequence is not taking account of the primary key values that were explicitly set when the fixture was loaded.  If I use psql to insert rows with pk specified, then insert rows without pk specified, the sequence handles it fine.  I tested with Postgresql (8.1) using both the postgresql and postgresql_psycopg2 database engines.

Here's some example code to reproduce the problem (also app attached).

demo/models.py

{{{
#!python
from django.db import models

class Example(models.Model):
    name = models.CharField(maxlength=100)
}}}

demo/fixtures/example.json

{{{
[
    {
        ""pk"": 1,
        ""model"": ""demo.example"",
        ""fields"":
        {
            ""name"": ""example 1""
        }
    }
]
}}}

demo/tests.py

{{{
#!python
from django.test import TestCase

from demo.models import Example

class ExampleTestCase(TestCase):
    fixtures = ['example']
    
    def test_example(self):
        new_example = Example.objects.create(name='new example')
}}}


./manage.py test demo

Error message:

{{{
======================================================================
ERROR: test_example (project.demo.tests.ExampleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/home/user/dev/project/web/project/../project/demo/tests.py"", line 10, in test_example
    new_example = Example.objects.create(name='new example')
  File ""/usr/lib/python2.4/site-packages/django/db/models/manager.py"", line 79, in create
    return self.get_query_set().create(**kwargs)
  File ""/usr/lib/python2.4/site-packages/django/db/models/query.py"", line 262, in create
    obj.save()
  File ""/usr/lib/python2.4/site-packages/django/db/models/base.py"", line 238, in save
    ','.join(placeholders)), db_values)
IntegrityError: duplicate key violates unique constraint ""demo_example_pkey""
}}}"		closed	Testing framework	dev		fixed	test fixture pk primary key		Accepted	0	0	0	0	0	0
