Click here to Skip to main content
15,117,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I run python manage.py tests

I get this error as django.db.utils.IntegrityError: UNIQUE constraint failed: books_book.id

it causes most of the other tests to fail

What I have tried:

Here are the full tests.py file:
Python
class BookViewTests(TestCase):
        self.user=get_user_model().objects.create_user(
            username='test',
            email='test@gmail.com',
            city='city',
            state='state',
            password='secret',
            id_2 ='96540845',
            work = 'Wipro'
            )
      
        self.book =Book.objects.create(
            title='How to Win Friends and Influence People',
            author='Dale Carnegie',
            genre ='Self-help',
            owner =self.user,
            id ='75777553',
            is_approved=False,
            available=False
        )
    #book detail view
    def test_book_detail_view(self):
        self.client.login(username='test',password='secret')
        resp =self.client.get(reverse('book_detail',kwargs={'book_id':75777553}))
        self.assertEqual(resp.status_code,200)
        self.assertTemplateUsed(resp,'book_detail.html')



My model:


Python
class Book(models.Model):
    id=models.SlugField(max_length=8,primary_key=True)
    genre_choices=[(i,i) for i in ['Biography','Business','Children','Classic','Comedy','Fantasy','History','Horror','Mystery','Mythology','Poetry','Religion','Romance','SciFi','Self-Help','Spirituality','Thriller','Non-Fiction','Fiction','Others']]
    title =models.CharField(max_length =255)
    author =models.CharField(max_length =255,validators=[alphabet_validate])
    genre =models.CharField(max_length=100,choices=genre_choices)
    available =models.BooleanField(default =True)
    slug = models.SlugField(verbose_name='Slug', null=True,blank=True,max_length = 200)


How to solve this issue? Thanks in Advance.
Posted
Updated 13-Aug-21 7:18am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900