Add database table-level constraints to your Django model's Meta
Project description
# 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',
…
]
```
## 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',
…
]
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-db-constraints-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed93690399ed4240ad7fcd2e314221439bb2bd38b73a70e74b374702747f57c2 |
|
MD5 | 55cd836630df9a240f5040aead939904 |
|
BLAKE2b-256 | b06c6e82abc837fc654b035266707ff87b8d493041a571d731486e56e1491d91 |
Close
Hashes for django_db_constraints-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1113f5a09b9e6811b54136d5f65b7bdf14ce057adc06a0e18447b3414849d744 |
|
MD5 | e805670ba6fcbfabbb9ee8036c66e7e1 |
|
BLAKE2b-256 | d8fef7ce761fd0895c1d99435947ed5077dfbe82348ccaa3fef2c9cc80e6a577 |