From 7894233e904289d9f093103efec2d92fc14c6c43 Mon Sep 17 00:00:00 2001
From: Julie Pichon <julie@jpichon.net>
Date: Sat, 25 Jun 2011 17:17:35 +0100
Subject: [PATCH] 15884: Test for not allowing nullable primary key fields
---
django/core/management/validation.py | 2 ++
tests/modeltests/invalid_models/models.py | 4 ++++
2 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/django/core/management/validation.py b/django/core/management/validation.py
index bb4fa55..4f08f28 100644
a
|
b
|
def get_validation_errors(outfile, app=None):
|
40 | 40 | e.add(opts, '"%s": You can\'t use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.' % f.name) |
41 | 41 | if f.name.endswith('_'): |
42 | 42 | e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name) |
| 43 | if f.primary_key and f.null: |
| 44 | e.add(opts, '"%s": Primary key fields do not accept null values.' % f.name) |
43 | 45 | if isinstance(f, models.CharField): |
44 | 46 | try: |
45 | 47 | max_length = int(f.max_length) |
diff --git a/tests/modeltests/invalid_models/models.py b/tests/modeltests/invalid_models/models.py
index ab9edd6..6ace5a2 100644
a
|
b
|
class ArticleAttachment(models.Model):
|
234 | 234 | tags = generic.GenericRelation(TaggedObject) |
235 | 235 | user_tags = generic.GenericRelation(UserTaggedObject) |
236 | 236 | |
| 237 | class PrimaryKeyNull(models.Model): |
| 238 | my_pk_field = models.IntegerField(primary_key=True, null=True) |
| 239 | |
237 | 240 | model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer. |
238 | 241 | invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer. |
239 | 242 | invalid_models.fielderrors: "charfield3": CharFields require a "max_length" attribute that is a positive integer. |
… |
… |
invalid_models.nonexistingorderingwithsingleunderscore: "ordering" refers to "do
|
344 | 347 | invalid_models.invalidsetnull: 'fk' specifies on_delete=SET_NULL, but cannot be null. |
345 | 348 | invalid_models.invalidsetdefault: 'fk' specifies on_delete=SET_DEFAULT, but has no default value. |
346 | 349 | invalid_models.articleattachment: Model 'UserTaggedObject' must have a GenericForeignKey in order to create a GenericRelation that points to it. |
| 350 | invalid_models.primarykeynull: "my_pk_field": Primary key fields do not accept null values. |
347 | 351 | """ |