#15884 closed Bug (fixed)
Model validation allows nullable primary key field.
Reported by: | JustinTArthur | Owned by: | JustinTArthur |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | julie@…, alexandrul | 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
If I have a primary key field that is also nullable, the model validation code is currently allowing it:
class MyModel(models.Model): other_model = OneToOneField(OtherModel, primary_key=True, null=True)
This behavior seems like a defect given two circumstances:
- No SQL implementation I know of allows a primary key column to be
NULL
in the schema. - Django's
CASCADE
deletion willNone
/NULL
out anynull=True
foreign keys (see source:/trunk/django/db/models/deletion.py@15927#L19),- While a SQL backend might correctly map the
None
to a0
instead ofNULL
in theUPDATE
statement, after a second deletion against the same table, you would have more than one row with primary key of0
, thus violating the uniqueness constraint intrinsic to a primary key field.
- While a SQL backend might correctly map the
Attachments (2)
Change History (9)
comment:1 by , 14 years ago
Status: | new → assigned |
---|
by , 14 years ago
Attachment: | ticket_15884-patch_1.diff added |
---|
Patch to add validation error when a model field is both a primary key and nullable.
comment:2 by , 14 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Has patch: | set |
comment:3 by , 14 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
by , 13 years ago
Attachment: | 15884.diff added |
---|
comment:4 by , 13 years ago
Cc: | added |
---|---|
Needs tests: | unset |
UI/UX: | unset |
I'm attaching a patch with a test for the new validation behaviour -- the patch also includes JustinTArthur's diff as there was a missing double-quote around the field name.
comment:5 by , 13 years ago
Cc: | added |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Note:
See TracTickets
for help on using tickets.
The correct link for the
CASCADE
interest point is actually source:/django/trunk/django/db/models/deletion.py@15927#L19