Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Teach "django-admin.py validate" to forbid nullable primary keys.

Fixes #15884, with thanks to JustinTArthur and Julie Pichon.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16678 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 75ddbf77a62b804740e887d940ded2f8c0bc1087 1 parent 8b87a94
Malcolm Tredinnick authored August 23, 2011
2  django/core/management/validation.py
@@ -39,6 +39,8 @@ def get_validation_errors(outfile, app=None):
39 39
                 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)
40 40
             if f.name.endswith('_'):
41 41
                 e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name)
  42
+            if f.primary_key and f.null:
  43
+                e.add(opts, '"%s": Primary key fields cannot have null=True.' % f.name)
42 44
             if isinstance(f, models.CharField):
43 45
                 try:
44 46
                     max_length = int(f.max_length)
7  tests/modeltests/invalid_models/invalid_models/models.py
@@ -220,7 +220,8 @@ class InvalidSetDefault(models.Model):
220 220
     fk = models.ForeignKey('self', on_delete=models.SET_DEFAULT)
221 221
 
222 222
 class UnicodeForeignKeys(models.Model):
223  
-    """Foreign keys which can translate to ascii should be OK, but fail if they're not."""
  223
+    """Foreign keys which can translate to ascii should be OK, but fail if
  224
+    they're not."""
224 225
     good = models.ForeignKey(u'FKTarget')
225 226
     also_good = models.ManyToManyField(u'FKTarget', related_name='unicode2')
226 227
 
@@ -228,6 +229,9 @@ class UnicodeForeignKeys(models.Model):
228 229
     # when adding the errors in core/management/validation.py
229 230
     #bad = models.ForeignKey(u'★')
230 231
 
  232
+class PrimaryKeyNull(models.Model):
  233
+    my_pk_field = models.IntegerField(primary_key=True, null=True)
  234
+
231 235
 
232 236
 model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer.
233 237
 invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer.
@@ -338,4 +342,5 @@ class UnicodeForeignKeys(models.Model):
338 342
 invalid_models.nonexistingorderingwithsingleunderscore: "ordering" refers to "does_not_exist", a field that doesn't exist.
339 343
 invalid_models.invalidsetnull: 'fk' specifies on_delete=SET_NULL, but cannot be null.
340 344
 invalid_models.invalidsetdefault: 'fk' specifies on_delete=SET_DEFAULT, but has no default value.
  345
+invalid_models.primarykeynull: "my_pk_field": Primary key fields cannot have null=True.
341 346
 """

0 notes on commit 75ddbf7

Please sign in to comment.
Something went wrong with that request. Please try again.