Skip to main content
# django-db-constraints

## What is this?

Add database table-level constraints to your Django model's Meta class and have `makemigrations` add the appropriate migration.

```python
class Foo(models.Model):
bar = models.IntegerField()
baz = models.IntegerField()

class Meta:
db_constraints = {
'bar_equal_baz': 'check (bar = baz)',
}
```

This should generate a migration like so:

```python
class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Foo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bar', models.IntegerField()),
('baz', models.IntegerField()),
],
),
django_db_constraints.operations.AlterConstraints(
name='Foo',
db_constraints={'bar_equal_baz': 'check (bar = baz)'},
),
]
```

The resulting SQL applied:

```sql
CREATE TABLE "sample_foo" ("id" serial NOT NULL PRIMARY KEY, "bar" integer NOT NULL, "baz" integer NOT NULL)
ALTER TABLE "sample_foo" ADD CONSTRAINT "bar_equal_baz" check (bar = baz)
```

## Composite foreign keys

It's possible to support composite foreign keys if you have a unique key on your reference model:

([Why are composite foreign keys useful?](https://github.com/rapilabs/blog/blob/master/articles/same-parent-db-pattern.md))

```python
class Bar(models.Model):
baz = models.IntegerField()

class Meta:
unique_together = ('id', 'baz')


class Foo(models.Model):
bar = models.ForeignKey(Bar)
baz = models.IntegerField()

class Meta:
db_constraints = {
'composite_fk': 'foreign key (bar_id, baz) references sample_bar (id, baz)',
}
```

Results in:

```sql
ALTER TABLE "sample_foo" ADD CONSTRAINT "composite_fk" foreign key (bar_id, baz) references sample_bar (id, baz)
```

## Migration operation ordering

Given that nothing will depend on a constraint operation, they're simply added to the end of the list of operations
for a migration. This includes operations that drop fields used in a constraint as the database drop will any related
constraints as well (at least with PostgreSQL).

## Caveats

It's possible to end up in a situation where the constraints are declared on the Meta class but do not exist in the database
due to a database dropping a constraint implicitly when a field in the constraint is dropped.

## Installation

```
pip install django-db-constraints
```

in your settings.py:

```python
INSTALLED_APPS = [
'django_db_constraints',
…
]
```


Metadata

Release files for django-db-constraints 0.3.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-db-constraints 0.3.0
File Size Uploaded
django-db-constraints-0.3.0.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-db-constraints 0.3.0
File Interpreter ABI Platform
django_db_constraints-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / django-db-constraints-0.3.0.tar.gz

Download URL django-db-constraints-0.3.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ed93690399ed4240ad7fcd2e314221439bb2bd38b73a70e74b374702747f57c2
BLAKE2b-256 checksum
How to use checksums
b06c6e82abc837fc654b035266707ff87b8d493041a571d731486e56e1491d91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_db_constraints-0.3.0-py3-none-any.whl

Download URL django_db_constraints-0.3.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1113f5a09b9e6811b54136d5f65b7bdf14ce057adc06a0e18447b3414849d744
BLAKE2b-256 checksum
How to use checksums
d8fef7ce761fd0895c1d99435947ed5077dfbe82348ccaa3fef2c9cc80e6a577
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

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