Opened 17 years ago
Closed 16 years ago
#5671 closed (fixed)
Change SQL generation when primary_key=True and unique=True
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | db-be-api | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
At the moment, these two models, which are logically equivalent, generate different sql:
class Country(models.Model): isocode = CharField(max_length=3, primary_key=True, unique=True)
class Country(models.Model): isocode = CharField(max_length=3, primary_key=True)
- at least for backends other than Oracle
I would like to suggest that we do not create explicit UNIQUE constraints for (single field) primary keys.
On the other hand, I think it would be nice if field.unique returned True for a primary key field no matter if it was defined as such or not.
I will write a patch doing this, but am of course open to alternative suggestions.
Attachments (2)
Change History (7)
by , 17 years ago
Attachment: | unique.diff added |
---|
by , 17 years ago
Attachment: | unique.2.diff added |
---|
Patch doing both of the things mentioned in the ticket
comment:1 by , 17 years ago
Has patch: | set |
---|
Oops - double submit. The patches are identical.
The patch does the following:
If a field is declared as primary_key, it is automatically flagged as unique
If a field is declared as primary_key, no UNIQUE is output in the SQL.
All checks of the type
if f.primary_key or f.unique:
are replaced by
if f.unique:
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:3 by , 17 years ago
Triage Stage: | Design decision needed → Accepted |
---|
The concept seems reasonable. The patch introduces some random formatting changes that are not part of the change needed, but otherwise looks reasonable from just eyeballing it. I haven't tested it on all (or, in fact, any) db backends, so I'm not going to move it to almost ready-for-checkin just yet.
comment:4 by , 16 years ago
Keywords: | db-be-api added |
---|
comment:5 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch doing both of the things mentioned in the article