December 2018
Beginner to intermediate
404 pages
10h 14m
English
SQL constraints are added to the database table definition and are enforced directly by PostgreSQL. They are defined using the _sql_constraints class attribute.
It is a list of tuples, and each tuple has the format (name, code, error):
We will add two SQL constraints to the Book Model. One is a unique constraint ensuring that we don't have repeated books with the same title and publication date; the other is to check that the publication date is not in the future:
# class Book(models.Model):
_sql_constraints = [
('library_book_name_date_uq', # Constraint ...Read now
Unlock full access