views:

88

answers:

1

Hi folks,

(Worth starting off with a disclaimer: I'm very new to PostGreSQL)

I have a django site which involves a standard app/tests.py testing file. If I migrate the DB to MySQL (through South),, the tests all pass.

However in PostGresQL, I'm getting the following error:

IntegrityError: duplicate key value violates unique constraint "business_contact_pkey"

Note this happens while unit testing only - the actual page runs fine in both MySQL & PostGresql.

Really having a heckuva time figuring this one out. Anyone have ideas? Below are the Postgresql "\d business_contact" & offending tests.py method if they help. No changes made to either DB except the (same) South migrations

Thanks

 first_name   | character varying(200)   | not null
 mobile_phone | character varying(100)   | 
 surname      | character varying(200)   | not null
 business_id  | integer                  | not null
 created      | timestamp with time zone | not null
 deleted      | boolean                  | not null default false
 updated      | timestamp with time zone | not null
 slug         | character varying(150)   | not null
 phone        | character varying(100)   | 
 email        | character varying(75)    | 
 id           | integer                  | not null default nextval('business_contact_id_seq'::regclass)
Indexes:
    "business_contact_pkey" PRIMARY KEY, btree (id)
    "business_contact_slug_key" UNIQUE, btree (slug)
    "business_contact_business_id" btree (business_id)
Foreign-key constraints:
    "business_id_refs_id_772cc1b7b40f4b36" FOREIGN KEY (business_id) REFERENCES business(id) DEFERRABLE INITIALLY DEFERRED
Referenced by:
    TABLE "business" CONSTRAINT "primary_contact_id_refs_id_dfaf59c4041c850" FOREIGN KEY (primary_contact_id) REFERENCES business_contact(id) DEFERRABLE INITIALLY DEFERRED

TEST DEF:

   def test_add_business_contact(self):
        """ Add a business contact """
        contact_slug = 'test-new-contact-added-new-adf'
        business_id = 1
        business = Business.objects.get(id=business_id)
        postdata = {
                'first_name': 'Test',
                'surname': 'User',
                'business': '1',
                'slug': contact_slug,
                'email': '[email protected]',
                'phone': '12345678',
                'mobile_phone': '9823452',
                'business': 1,
                'business_id': 1,
                }

        #Test to ensure contacts that should not exist are not returned
        contact_not_exists = Contact.objects.filter(slug=contact_slug)
        self.assertFalse(contact_not_exists)

        #Add the contact and ensure it is present in the DB afterwards """
        contact_add_url = '%s%s/contact/add/' % (settings.BUSINESS_URL, business.slug)
        self.client.post(contact_add_url, postdata)
        added_contact = Contact.objects.filter(slug=contact_slug)
        print added_contact

        try:
            self.assertTrue(added_contact)
        except:
            formset = ContactForm(postdata)
            print formset.errors
            self.assertFalse(True, "Contact not found in the database - most likely, the post values in the test didn't validate against the form")
+2  A: 

Found the answer - thought I'd leave it here for posterity.

In South, I was explicitly setting primary key IDs for datamigrations. PGSQL's sequencer wasn't picking them up and was trying to use PK values which had already been set in the datamigrations.

Removing the explicitly-set primary key values from the datamigrations solved the issue.

unclaimedbaggage