{"id":13468829,"url":"https://github.com/shangxiao/django-db-constraints","last_synced_at":"2026-03-08T16:37:48.742Z","repository":{"id":50406828,"uuid":"99153689","full_name":"shangxiao/django-db-constraints","owner":"shangxiao","description":"Add database table-level constraints to your Django model's Meta","archived":false,"fork":false,"pushed_at":"2020-04-01T18:01:26.000Z","size":10,"stargazers_count":45,"open_issues_count":8,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-18T07:46:58.660Z","etag":null,"topics":["constraints","database","django"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shangxiao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-02T19:31:45.000Z","updated_at":"2025-03-02T21:32:17.000Z","dependencies_parsed_at":"2022-09-15T21:51:29.969Z","dependency_job_id":null,"html_url":"https://github.com/shangxiao/django-db-constraints","commit_stats":null,"previous_names":["rapilabs/django-db-constraints"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shangxiao%2Fdjango-db-constraints","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shangxiao%2Fdjango-db-constraints/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shangxiao%2Fdjango-db-constraints/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shangxiao%2Fdjango-db-constraints/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shangxiao","download_url":"https://codeload.github.com/shangxiao/django-db-constraints/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245597313,"owners_count":20641865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constraints","database","django"],"created_at":"2024-07-31T15:01:19.922Z","updated_at":"2025-12-13T20:01:59.256Z","avatar_url":"https://github.com/shangxiao.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# django-db-constraints\n\n## What is this?\n\nAdd database table-level constraints to your Django model's Meta class and have `makemigrations` add the appropriate migration.\n\n```python\nclass Foo(models.Model):\n    bar = models.IntegerField()\n    baz = models.IntegerField()\n\n    class Meta:\n        db_constraints = {\n            'bar_equal_baz': 'check (bar = baz)',\n        }\n```\n\nThis should generate a migration like so:\n\n```python\nclass Migration(migrations.Migration):\n\n    initial = True\n\n    dependencies = [\n    ]\n\n    operations = [\n        migrations.CreateModel(\n            name='Foo',\n            fields=[\n                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),\n                ('bar', models.IntegerField()),\n                ('baz', models.IntegerField()),\n            ],\n        ),\n        django_db_constraints.operations.AlterConstraints(\n            name='Foo',\n            db_constraints={'bar_equal_baz': 'check (bar = baz)'},\n        ),\n    ]\n```\n\nThe resulting SQL applied:\n\n```sql\nCREATE TABLE \"sample_foo\" (\"id\" serial NOT NULL PRIMARY KEY, \"bar\" integer NOT NULL, \"baz\" integer NOT NULL)\nALTER TABLE \"sample_foo\" ADD CONSTRAINT \"bar_equal_baz\" check (bar = baz)\n```\n\n## Composite foreign keys\n\nIt's possible to support composite foreign keys if you have a unique key on your reference model:\n\n([Why are composite foreign keys useful?](https://github.com/rapilabs/blog/blob/master/articles/same-parent-db-pattern.md))\n\n```python\nclass Bar(models.Model):\n    baz = models.IntegerField()\n\n    class Meta:\n        unique_together = ('id', 'baz')\n\n\nclass Foo(models.Model):\n    bar = models.ForeignKey(Bar)\n    baz = models.IntegerField()\n\n    class Meta:\n        db_constraints = {\n            'composite_fk': 'foreign key (bar_id, baz) references sample_bar (id, baz)',\n        }\n```\n\nResults in:\n\n```sql\nALTER TABLE \"sample_foo\" ADD CONSTRAINT \"composite_fk\" foreign key (bar_id, baz) references sample_bar (id, baz)\n```\n\n## Notes\n\n### Migration operation ordering\n\nGiven that nothing will depend on a constraint operation, they're simply added to the end of the list of operations\nfor a migration.  This includes operations that drop fields used in a constraint as the database drop will any related\nconstraints as well (at least with PostgreSQL).\n\n### Caveats\n\nIt's possible to end up in a situation where the constraints are declared on the Meta class but do not exist in the database\ndue to a database dropping a constraint implicitly when a field in the constraint is dropped.\n\n### Please test your constraints!\n\nI encourage folks to write tests for their constraints to ensure they write are actually applied in the database.\n\n### Acknowledgements\n\nThanks to @schinckel and @MarkusH for their advice and ideas.\n\n## Installation\n\n```\npip install django-db-constraints\n```\n\nin your settings.py:\n\n```python\nINSTALLED_APPS = [\n    'django_db_constraints',\n    …\n]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshangxiao%2Fdjango-db-constraints","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshangxiao%2Fdjango-db-constraints","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshangxiao%2Fdjango-db-constraints/lists"}