#11964 closed New feature (fixed)
Add the ability to use database-level CHECK CONSTRAINTS
| Reported by: | Matthew Schinckel | Owned by: | Ian Foote |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | master |
| Severity: | Normal | Keywords: | check constraint |
| Cc: | Danilo Bargen, esigra, gam_phon, python@…, Ryan Hiebert, Michael Wheeler, Pablo Montepagano, Ties de Kock | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I mentioned adding database level CHECK CONSTRAINTS in a post on django-developers.
http://groups.google.com/group/django-developers/browse_thread/thread/38937992972c7808
Attached to this ticket is a patch that provides the functionality listed in the first post of this thread. This is not complete: it only allows simple constraints, and is missing a complete test suite.
Attachments (1)
Change History (34)
Changed 9 years ago by
| Attachment: | check_constraints.diff added |
|---|
comment:1 Changed 9 years ago by
| Triage Stage: | Unreviewed → Someday/Maybe |
|---|
If we do this, it should be tied into the model validation framework in some way.
comment:2 Changed 9 years ago by
Agreed. The initial patch was made before the changes with model validation had hit. I will get back to this at some stage.
comment:3 Changed 7 years ago by
| Severity: | → Normal |
|---|---|
| Type: | → New feature |
comment:6 Changed 6 years ago by
| Cc: | Danilo Bargen added |
|---|
comment:7 Changed 4 years ago by
| Patch needs improvement: | set |
|---|
comment:8 Changed 3 years ago by
| Cc: | esigra added |
|---|
comment:9 Changed 3 years ago by
| Summary: | Using database-level CHECK CONSTRAINTS → Add the ability to use database-level CHECK CONSTRAINTS |
|---|---|
| Triage Stage: | Someday/Maybe → Accepted |
| Version: | 1.1 → master |
comment:10 Changed 3 years ago by
| Cc: | gam_phon added |
|---|
comment:11 Changed 2 years ago by
Could/should this feature request also handle this case of constraint I brought up in this thread?
https://code.djangoproject.com/ticket/11964
Basically unique_together doesn't consider a null value unique per SQL spec -- which makes perfect sense in single column indexes and is extended to multi column indexes.
So if someone wanted (A, B, NULL) to be a unique row there would need to be an index built on the first two columns "where <third column> is null"
These SO articles give a better overview:
http://stackoverflow.com/questions/17510261/django-unique-together-constraint-failure
http://dba.stackexchange.com/questions/9759/postgresql-multi-column-unique-constraint-and-null-values
comment:13 Changed 2 years ago by
| Cc: | python@… added |
|---|---|
| Owner: | changed from nobody to Ian Foote |
| Status: | new → assigned |
comment:16 Changed 19 months ago by
| Needs documentation: | set |
|---|
comment:17 Changed 14 months ago by
| Cc: | Ryan Hiebert added |
|---|
comment:18 Changed 14 months ago by
| Needs documentation: | unset |
|---|---|
| Patch needs improvement: | unset |
comment:19 Changed 12 months ago by
| Patch needs improvement: | set |
|---|
comment:20 Changed 11 months ago by
This external project seems to implement this feature:
https://github.com/rapilabs/django-db-constraints
It does some monkey patching that would be unnecessary if this were in core.
comment:21 Changed 11 months ago by
| Patch needs improvement: | unset |
|---|
comment:22 Changed 10 months ago by
Now that Django 2 is released, I'd like to see this land. I think I've resolved all the outstanding issues raised on the pull request before. Is the next step to rebase/squash my work in progress commits?
comment:23 Changed 8 months ago by
| Patch needs improvement: | set |
|---|
conflicts needed to be resolved and squash for final review I believe.
comment:24 Changed 8 months ago by
| Patch needs improvement: | unset |
|---|
Conflicts fixed and commits squashed.
comment:25 Changed 7 months ago by
| Cc: | Michael Wheeler added |
|---|
comment:26 Changed 6 months ago by
| Patch needs improvement: | set |
|---|
comment:27 Changed 6 months ago by
| Patch needs improvement: | unset |
|---|
Please don't check "Patch needs improvement" only because there's a small merge conflict (as far as I can see, there's no outstanding review comments on the patch).
comment:28 Changed 5 months ago by
| Cc: | Pablo Montepagano added |
|---|
comment:29 Changed 4 months ago by
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:30 Changed 4 months ago by
| Cc: | Ties de Kock added |
|---|---|
| Keywords: | constraint added; contsraint removed |
comment:32 Changed 2 months ago by
Is it now possible to define a field that comes with a check constraint, or can they only be defined at the table level? I'd like to be able to define my own StrictlyPositiveIntegerField.
Contains patches against trunk that enable check constraints