---
source: https://iinventedlight.com/library/notes/django-model-constraints.md
title: "Declare Django CheckConstraint and UniqueConstraint on Meta.constraints"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Django docs); Public-Domain (SQLite); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Django docs, Constraints reference; SQLite, CREATE TABLE. BSD-3-Clause; Public-Domain."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Django model CheckConstraint and UniqueConstraint"
tags: [django, constraints, CheckConstraint, UniqueConstraint, sqlite, orm]
client: Librarian
sources:
  - id: S1
    title: "Constraints reference"
    publisher: Django docs
    url: https://docs.djangoproject.com/en/5.2/ref/models/constraints/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "CREATE TABLE"
    publisher: SQLite
    url: https://sqlite.org/lang_createtable.html
    accessed: 2026-11-19
    license: "Public-Domain"
---
# 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

- [django-models](https://iinventedlight.com/library/notes/django-models/)
- [django-migrations](https://iinventedlight.com/library/notes/django-migrations/)
- [postgres-constraints-foreign-keys](https://iinventedlight.com/library/notes/postgres-constraints-foreign-keys/)

## Claims

- id: C1
  claim: Django constraints go on the model Meta.constraints option.
  source: S1
  evidence: "They are added in the model Meta.constraints option."

- id: C3
  claim: CheckConstraint creates a check constraint in the database.
  source: S1
  evidence: "Creates a check constraint in the database."

- id: C4
  claim: SQLite supports UNIQUE, NOT NULL, CHECK and FOREIGN KEY constraints.
  source: S2
  evidence: "SQLite supports UNIQUE, NOT NULL, CHECK and FOREIGN KEY constraints."

## Sources

- [S1] Constraints reference. Django docs. https://docs.djangoproject.com/en/5.2/ref/models/constraints/ . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] CREATE TABLE. SQLite. https://sqlite.org/lang_createtable.html . License: Public-Domain. Accessed 2026-11-19.

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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
