Django unique constraint fails
I am creating a book model, and with that model, there is a isbn variable that should have the unique constraint set to true which should stop there from being duplicate isbn values in my database. however when unit testing this model, the test_isbn_must_be_unique() test fails to raise the validation error even though I have set unique to be true in the book model.
I was expecting the test to pass since I had set unique to be true in the list of constraints for the isbn variable. however, the test fails.
`# models.py file
from django.core.validators import RegexValidator
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
username = models.CharField(
max_length=30,
unique=True,
validators=[RegexValidator(
regex=r'^@\w{3,}$',
message='Username must consist of @ followed by at least three alphanumericals'
)]
)
first_name = models.CharField(max_length=50, blank=False)
last_name = models.CharField(max_length=50, blank=False)
email = models.EmailField(unique=True, blank=False)
bio = models.CharField(max_length=520, blank=True)
class Book(models.Model):
isbn = models.CharField(max_length=13, unique=True, null=False, blank=False)
book_title = models.TextField(blank=False, max_length=500)
book_author = models.CharField(blank=False, max_length=255)
year_of_publication = models.CharField(max_length=13, blank=False)
publishers = models.CharField(blank=False, max_length=255)
image_url_s = models.CharField(blank=False, max_length=255)
image_url_m = models.CharField(blank=False, max_length=255)
image_url_l = models.CharField(blank=False, max_length=255)`
`"Testing Book Model"
from django.test import TestCase
from django.core.exceptions import ValidationError
from clubs.models import Book
class BookModelTestCase(TestCase):
def setUp(self):
super(TestCase, self).setUp()
self.book = Book(
isbn='0195153448',
book_title='Classical Mythology',
book_author='Mark P. O. Morford',
year_of_publication='2002',
publishers='Oxford University Press',
image_url_s='http://images.amazon.com/images/P/0195153448.01.THUMBZZZ.jpg',
image_url_m='http://images.amazon.com/images/P/0195153448.01.MZZZZZZZ.jpg',
image_url_l='http://images.amazon.com/images/P/0195153448.01.LZZZZZZZ.jpg'
)
def test_valid_book(self):
self._assert_book_is_valid()
def test_isbn_must_not_be_blank(self):
self.book.isbn = None
self._assert_book_is_invalid()
def test_isbn_must_not_be_over_13_numbers(self):
self.book.isbn = '1' * 14
with self.assertRaises(ValidationError):
self.book.full_clean()
def test_isbn_can_be_13_numbers(self):
self.book.isbn = '1' * 13
self._assert_book_is_valid()
def test_isbn_must_be_unique(self):
second_book = self._create_second_book()
self.book.isbn = second_book.isbn
self._assert_book_is_invalid()
def _create_second_book(self):
book = Book(
isbn='0002005018',
book_title='Clara Callan',
book_author='Richard Bruce Wright',
year_of_publication='2001',
publishers='HarperFlamingo Canada',
image_url_s='http://images.amazon.com/images/P/0002005018.01.THUMBZZZ.jpg',
image_url_m='http://images.amazon.com/images/P/0002005018.01.MZZZZZZZ.jpg',
image_url_l='http://images.amazon.com/images/P/0002005018.01.LZZZZZZZ.jpg'
)
return book
def _assert_book_is_valid(self):
try:
self.book.full_clean()
except ValidationError:
self.fail('user should be valid')
def _assert_book_is_invalid(self):
with self.assertRaises(ValidationError):
self.book.full_clean()
`