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.apps.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)
```
## 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.apps.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)
```
## 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.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27cbdcd38118e37834600cd0b5b8ec171cdb7ad387d8400eba67d112078a144b |
|
MD5 | aefa6c2530eee05c938c2b4610e4c7f2 |
|
BLAKE2b-256 | 04759ec62fbb94df67ad9a4d7b428f4572739e25447e0fcfc27ef5705cc875e4 |
Close
Hashes for django_db_constraints-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c98a9fd6b2057dd89a7404510b18ade72c3f6f7c465134cd93e645a6ec76841 |
|
MD5 | 6e1e2793d8a06c2c75c43aa262129612 |
|
BLAKE2b-256 | 58a4c4825038e7e320f7140850852c900419f3dafd133f4f780bc9e3aeba9f36 |