[Django]-IntegrityError: (1062, Duplicate entry for key)

7👍

Django automatically creates few default permissions on every model, these are: add, delete and change. You are getting integrity error because you are trying to create permissions with the same name. Just remove delete_*** from your Meta description and everything should be ok.

👤Ski

0👍

The accepted answer solves the original question and is a good starting point for others that have come to this page looking for the particular error.

To add to this for future reference, in case this is not an issue of the specific permissions but a general fixture loading order issue, this can be solved with using call_command:

from django.test import TestCase
from django.core.management import call_command


class Tests(TestCase):
    @classmethod
    def setUpTestData(cls):
        # do some early data setup
        ...
        # then load data
        call_command('loaddata', 'myfixture', verbosity=0)

    def mytest(self):
        # some tests in here
        ...
👤Wtower

Leave a comment