Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Tests: setUpTestData on Postgres throws: "Duplicate key value violates unique constraint"

I am running into a database issue in my unit tests. I think it has something to do with the way I am using TestCase and setUpData.

When I try to set up my test data with certain values, the tests throw the following error:

django.db.utils.IntegrityError: duplicate key value violates unique constraint 

...

psycopg2.IntegrityError: duplicate key value violates unique constraint "InventoryLogs_productgroup_product_name_48ec6f8d_uniq"
DETAIL:  Key (product_name)=(Almonds) already exists.

I changed all of my primary keys and it seems to be running fine. It doesn't seem to affect any of the tests.

However, I'm concerned that I am doing something wrong. When it first happened, I reversed about an hour's worth of work on my app (not that much code for a noob), which corrected the problem.

Then when I wrote the changes back in, the same issue presented itself again. TestCase is pasted below. The issue seems to occur after I add the sortrecord items, but corresponds with the items above it.

I don't want to keep going through and changing primary keys and urls in my tests, so if anyone sees something wrong with the way I am using this, please help me out. Thanks!

TestCase

class DetailsPageTest(TestCase):


@classmethod
def setUpTestData(cls):

    cls.product1 = ProductGroup.objects.create(
                        product_name="Almonds"
                        )
    cls.variety1 = Variety.objects.create(
                        product_group = cls.product1,
                        variety_name = "non pareil",
                        husked = False,
                        finished = False,
                        )

    cls.supplier1 = Supplier.objects.create(
                        company_name = "Acme",
                        company_location = "Acme Acres",
                        contact_info = "Call me!"
                        )

    cls.shipment1 = Purchase.objects.create(
                        tag=9,
                        shipment_id=9999,
                        supplier_id = cls.supplier1,
                        purchase_date='2015-01-09',
                        purchase_price=9.99,
                        product_name=cls.variety1,
                        pieces=99,
                        kgs=999,
                        crackout_estimate=99.9
                        )
    cls.shipment2 = Purchase.objects.create(
                        tag=8,
                        shipment_id=8888,
                        supplier_id=cls.supplier1,
                        purchase_date='2015-01-08',
                        purchase_price=8.88,
                        product_name=cls.variety1,
                        pieces=88,
                        kgs=888,
                        crackout_estimate=88.8
                        )
    cls.shipment3 = Purchase.objects.create(
                        tag=7,
                        shipment_id=7777,
                        supplier_id=cls.supplier1,
                        purchase_date='2014-01-07',
                        purchase_price=7.77,
                        product_name=cls.variety1,
                        pieces=77,
                        kgs=777,
                        crackout_estimate=77.7
                        )

    cls.sortrecord1 = SortingRecords.objects.create(
                        tag=cls.shipment1,
                        date="2015-02-05",
                        bags_sorted=20,
                        turnout=199,
                        )

    cls.sortrecord2 = SortingRecords.objects.create(
                        tag=cls.shipment1,
                        date="2015-02-07",
                        bags_sorted=40,
                        turnout=399,
                        )
    cls.sortrecord3 = SortingRecords.objects.create(
                        tag=cls.shipment1,
                        date='2015-02-09',
                        bags_sorted=30,
                        turnout=299,
                        )

Models

from datetime import datetime

from django.db import models
from django.db.models import Q


class ProductGroup(models.Model):
    product_name = models.CharField(max_length=140, primary_key=True)

    def __str__(self):
        return self.product_name

    class Meta:
        verbose_name = "Product"

class Supplier(models.Model):
    company_name = models.CharField(max_length=45)
    company_location = models.CharField(max_length=45)
    contact_info = models.CharField(max_length=256)

    class Meta:
        ordering = ["company_name"]

    def __str__(self):
        return self.company_name

class Variety(models.Model):
    product_group = models.ForeignKey(ProductGroup)
    variety_name = models.CharField(max_length=140)
    husked = models.BooleanField()
    finished = models.BooleanField()
    description = models.CharField(max_length=500, blank=True)

    class Meta:
        ordering = ["product_group_id"]
        verbose_name_plural = "Varieties"

    def __str__(self):
        return self.variety_name


class PurchaseYears(models.Manager):

    def purchase_years_list(self):
        unique_years = Purchase.objects.dates('purchase_date', 'year')
        results_list = []
        for p in unique_years:
            results_list.append(p.year)
        return results_list


class Purchase(models.Model):
    tag = models.IntegerField(primary_key=True)
    product_name = models.ForeignKey(Variety, related_name='purchases')
    shipment_id = models.CharField(max_length=24)
    supplier_id = models.ForeignKey(Supplier)
    purchase_date = models.DateField()
    estimated_delivery = models.DateField(null=True, blank=True)
    purchase_price = models.DecimalField(max_digits=6, decimal_places=3)
    pieces = models.IntegerField()
    kgs = models.IntegerField()
    crackout_estimate = models.DecimalField(max_digits=6,decimal_places=3, null=True)
    crackout_actual = models.DecimalField(max_digits=6,decimal_places=3, null=True)
    objects = models.Manager()
    purchase_years = PurchaseYears()
    # Keep manager as "objects" in case admin, etc. needs it. Filter can be called like so:
    # Purchase.objects.purchase_years_list()
    # Managers in docs: https://docs.djangoproject.com/en/1.8/intro/tutorial01/

    class Meta:
        ordering = ["purchase_date"]

    def __str__(self):
        return self.shipment_id

    def _weight_conversion(self):
        return round(self.kgs * 2.20462)
    lbs = property(_weight_conversion)

class SortingModelsBagsCalulator(models.Manager):

    def total_sorted(self, record_date, current_set):
        sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
                  SortingRecords['date'] <= record_date]
        return sum(sorted)


class SortingRecords(models.Model):
    tag = models.ForeignKey(Purchase, related_name='sorting_record')
    date = models.DateField()
    bags_sorted = models.IntegerField()
    turnout = models.IntegerField()
    objects = models.Manager()

    def __str__(self):
        return "%s  [%s]" % (self.date, self.tag.tag)

    class Meta:
        ordering = ["date"]
        verbose_name_plural = "Sorting Records"

    def _calculate_kgs_sorted(self):
        kg_per_bag = self.tag.kgs / self.tag.pieces
        kgs_sorted = kg_per_bag * self.bags_sorted
        return (round(kgs_sorted, 2))
    kgs_sorted = property(_calculate_kgs_sorted)

    def _byproduct(self):
        waste = self.kgs_sorted - self.turnout
        return  (round(waste, 2))
    byproduct = property(_byproduct)

    def _bags_remaining(self):
        current_set = SortingRecords.objects.values().filter(~Q(id=self.id), tag=self.tag)
        sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
                  SortingRecords['date'] <= self.date]
        remaining = self.tag.pieces - sum(sorted) - self.bags_sorted
        return remaining
    bags_remaining = property(_bags_remaining)

EDIT

It also fails with integers, like so.

django.db.utils.IntegrityError: duplicate key value violates unique constraint "InventoryLogs_purchase_pkey"
DETAIL:  Key (tag)=(9) already exists.

UDPATE

So I should have mentioned this earlier, but I completely forgot. I have two unit test files that use the same data. Just for kicks, I matched a primary key in both instances of setUpTestData() to a different value and sure enough, I got the same error.

These two setups were working fine side-by-side before I added more data to one of them. Now, it appears that they need different values. I guess you can only get away with using repeat data for so long.

like image 966
Adam Starrh Avatar asked Jun 11 '15 09:06

Adam Starrh


1 Answers

I continued to get this error without having any duplicate data but I was able to resolve the issue by initializing the object and calling the save() method rather than creating the object via Model.objects.create()

In other words, I did this:

@classmethod
def setUpTestData(cls):
    cls.person = Person(first_name="Jane", last_name="Doe")
    cls.person.save()

Instead of this:

@classmethod
def setUpTestData(cls):
    cls.person = Person.objects.create(first_name="Jane", last_name="Doe")
like image 109
John R Perry Avatar answered Sep 20 '22 16:09

John R Perry