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.

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.1.0.tar.gz (21.6 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.1.0-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_pgconstraints-0.1.0.tar.gz
  • Upload date:
  • Size: 21.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 4e0fd0d38ba394d10b94dcaa57e37c6e3d35f37c1d5e9d5fed515bb75bf1c24e
MD5 06ab80326d2da6a3ffa74676116c5733
BLAKE2b-256 73ddf169e3f00548cf2e766edc4c9da610a465846d12dde03fcacb14d6e2b86c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_pgconstraints-0.1.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.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pgconstraints-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2632cda06cfd7d1cea3988a29f13bdfe428085bacc67862c556acfb50a18f7c8
MD5 3c14248d75b79a618c9b7a85e094e191
BLAKE2b-256 c59ef620cb8e8a8c71f0fc7bf316da0cb234e06320b157fecd096b92d28279c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_pgconstraints-0.1.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