← All notes

Declare Django CheckConstraint and UniqueConstraint on Meta.constraints

Answer: Put database constraints on a Django model's Meta.constraints option. CheckConstraint creates a check constraint in the database. UniqueConstraint creates a unique constraint in the database. On SQLite, those map onto the CHECK and UNIQUE constraints that CREATE TABLE supports.

How Django names them

The classes live in django.db.models.constraints, and they are imported into django.db.models for convenience. The usual import is from django.db import models, and then you refer to models.<Foo>Constraint. You must always specify a unique name for the constraint. You cannot normally put a constraint on an abstract base class, because Meta.constraints is inherited by subclasses with the same attribute values, including name, each time.

CheckConstraint and UniqueConstraint

CheckConstraint.condition is a Q object or boolean Expression. It specifies the check you want the constraint to enforce. A typical check ensures the age field is never less than 18. UniqueConstraint allows creating functional unique constraints on expressions and database fields. Constraints are checked during validation. validate() will do a query on the database to ensure that the constraint is respected.

| UNIQUE | NOT NULL | |---|---| | UNIQUE | Unique values in the columns | | NOT NULL | A NOT NULL constraint | | CHECK | CHECK constraints | | FOREIGN KEY | FOREIGN KEY constraints | | PRIMARY KEY | An optional PRIMARY KEY for the table |

SQLite supports UNIQUE, NOT NULL, CHECK and FOREIGN KEY constraints. The COLLATE and DEFAULT clauses are not really constraints: they do not restrict the data that the table may contain.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Django docs, Constraints reference; SQLite, CREATE TABLE. BSD-3-Clause; Public-Domain. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, Constraints reference; SQLite, CREATE TABLE. BSD-3-Clause; Public-Domain.

Cite this

Permanent ID: iil:note:y4ydej0e

iinventedlight. 2026. "Declare Django CheckConstraint and UniqueConstraint on Meta.constraints." iinventedlight Library, iil:note:y4ydej0e, version of 2026-11-19. https://iinventedlight.com/library/notes/django-model-constraints/. License: CC-BY-4.0.

@misc{iil_note_y4ydej0e,
  author = {{iinventedlight}},
  title = {{Declare Django CheckConstraint and UniqueConstraint on Meta.constraints}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/django-model-constraints/},
  note = {Permanent ID iil:note:y4ydej0e. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/django-model-constraints.md.