Skip to main content

django-pg-ddl-extras

A tiny library that implements declarative postgres function definitions for Django.

Requirements

  • Python >= 3.7
  • Django >= 3.2

Usage

In the below example, we create a function that is run as part of a constraint trigger.

from django_pg_ddl_extras import (
    PostgresTriggerFunctionDefinition,
    ConstraintTrigger,
    TriggerEvent,
)
from django.db import models
from django.db.models.constraints import Deferrable

# Write a custom constraint in SQL
# In order to get picked up by the migration engine, we include the function definition
# as part of the class `Meta.constraints` list.
# Unfortunately, Django does not seem to have a cleaner way to define this yet.
custom_function = PostgresTriggerFunctionDefinition(
    name="my_function",
    body="""
DECLARE
BEGIN
    IF (TG_OP = 'DELETE') THEN
        RETURN OLD;
    END IF;
    IF NOT FOUND THEN
        RAISE EXCEPTION
            'This is an example constraint error'
            USING ERRCODE = 23514;
    END IF;
    RETURN NEW;
END;
    """,
)

class MyModel(models.Model):
    class Meta:
        constraints = [
            custom_function,
            ConstraintTrigger(
                name="my_trigger",
                events=[TriggerEvent.UPDATE, TriggerEvent.INSERT, TriggerEvent.DELETE],
                deferrable=Deferrable.DEFERRED,
                function=custom_function.as_func(),
            ),
        ]

Release files for django-pg-ddl-extras 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-pg-ddl-extras 0.1.0
File Size Uploaded
django-pg-ddl-extras-0.1.0.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-pg-ddl-extras 0.1.0
File Interpreter ABI Platform
django_pg_ddl_extras-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.5 kB

Release files / django-pg-ddl-extras-0.1.0.tar.gz

Download URL django-pg-ddl-extras-0.1.0.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7717499cf184fe17bcf894853a813844c7ac3aa9656ad1704f2b7439ed7c89ed
BLAKE2b-256 checksum
How to use checksums
5410bf757adc99e36f18c5c1d41f1ca53309a2162f48e08b3942b78592c93875
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0a2 CPython/3.10.5 Linux/5.18.3-arch1-1

Release files / django_pg_ddl_extras-0.1.0-py3-none-any.whl

Download URL django_pg_ddl_extras-0.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9fd82658768168530980f8985b697737ba142ca3a6dee78bd415e8bdfb14d040
BLAKE2b-256 checksum
How to use checksums
8bdfc2d26364c0d22a77663e23cf54ae43a64ad91f193dfdf7333c7a7060c230
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0a2 CPython/3.10.5 Linux/5.18.3-arch1-1

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page