Skip to main content

Declarative PostgreSQL constraint triggers for Django

Project description

django-pgconstraints

PyPI version Python versions CI

Declarative PostgreSQL constraint triggers for Django, with foreign-key traversal that the built-in constraints can't express.

UniqueConstraint, CheckConstraint, and GeneratedField in Django 6.0 compile down to plain PostgreSQL constraints — so they can only reference columns on the same table. django-pgconstraints provides three trigger-based classes that accept F("related__field") expressions and compile to PL/pgSQL triggers via django-pgtrigger. Their API mirrors the Django equivalents to keep the mental model familiar.

from django.db.models import F, Q
from django_pgconstraints import CheckConstraintTrigger

class OrderLine(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.IntegerField()

    class Meta:
        triggers = [
            CheckConstraintTrigger(
                condition=Q(quantity__lte=F("product__stock")),
                name="orderline_qty_lte_stock",
            ),
        ]

What's in the box

  • UniqueConstraintTrigger — unique constraint whose fields= accepts FK chains ("book__author"). Also supports Django expressions, partial conditions, nulls_distinct, and deferred timing.
  • CheckConstraintTrigger — check constraint whose Q can cross foreign keys (Q(quantity__lte=F("product__stock"))).
  • GeneratedFieldTrigger — a computed column whose expression can reference related rows. The package automatically installs reverse triggers so the value stays in sync when that related data changes, and piggybacks RETURNING on save() / bulk_create() so the computed value lands on the Python instance with no extra query.

Each trigger also integrates with Django's Model.full_clean() so ORM-level validation still catches violations before they hit the database, and each accepts violation_error_code / violation_error_message so the errors raised by full_clean() match your application's error-handling conventions.

Install

pip install django-pgconstraints
# or
uv add django-pgconstraints

Add pgtrigger and django_pgconstraints to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "pgtrigger",
    "django_pgconstraints",
    ...
]

Triggers go in Meta.triggers (a django-pgtrigger extension), not Meta.constraints. The package ships a system check (pgconstraints.E001) that flags the common mistake.

Requirements

  • Python 3.14+
  • Django 6.0
  • PostgreSQL (any currently supported version)
  • django-pgtrigger 4.17+

Documentation

Full documentation at oliverhaas.github.io/django-pgconstraints.

Runnable examples live in examples/:

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pgconstraints-0.2.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pgconstraints-0.2.0-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file django_pgconstraints-0.2.0.tar.gz.

File metadata

  • Download URL: django_pgconstraints-0.2.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_pgconstraints-0.2.0.tar.gz
Algorithm Hash digest
SHA256 246d181777ae9c1b2e96f4ab4e690808bd29d4d34c9fef0ac0661146d9b61cdd
MD5 7c9585fb35177b37bd52d766275db80a
BLAKE2b-256 9e91bbb0930e3b325539089293bd5a89d198d0e00123a75f021816dd055593ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_pgconstraints-0.2.0.tar.gz:

Publisher: publish.yml on oliverhaas/django-pgconstraints

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_pgconstraints-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pgconstraints-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f57b31ebd2a70c6a43644d05ea56664297c1a107271534d2caec6c6b59ac85a
MD5 91388dc063f48b66e7bb02c9b9bab5a9
BLAKE2b-256 cbc47553e802ab21d5b87c89d3117a70e7d1f21935c03fdc9fa3734431601d5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_pgconstraints-0.2.0-py3-none-any.whl:

Publisher: publish.yml on oliverhaas/django-pgconstraints

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page