Model validation allows nullable primary key field.
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 will None
/NULL
out any null=True
foreign keys (see source:/trunk/django/db/models/deletion.py@15927#L19),
- While a SQL backend might correctly map the
None
to a 0
instead of NULL
in the UPDATE
statement, after a second deletion against the same table, you would have more than one row with primary key of 0
, thus violating the uniqueness constraint intrinsic to a primary key field.
Component: |
Uncategorized → Database layer (models, ORM)
|
Has patch: |
set
|
Needs tests: |
set
|
Triage Stage: |
Unreviewed → Accepted
|
Cc: |
julie@… added
|
Needs tests: |
unset
|
UI/UX: |
unset
|
Cc: |
alexandrul added
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
The correct link for the
CASCADE
interest point is actually source:/django/trunk/django/db/models/deletion.py@15927#L19