Skip to content

Commit

Permalink
Teach "django-admin.py validate" to forbid nullable primary keys.
Browse files Browse the repository at this point in the history
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
malcolmt committed Aug 23, 2011
1 parent 8b87a94 commit 75ddbf7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django/core/management/validation.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def get_validation_errors(outfile, app=None):
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) 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)
if f.name.endswith('_'): if f.name.endswith('_'):
e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name) e.add(opts, '"%s": Field names cannot end with underscores, because this would lead to ambiguous queryset filters.' % f.name)
if f.primary_key and f.null:
e.add(opts, '"%s": Primary key fields cannot have null=True.' % f.name)
if isinstance(f, models.CharField): if isinstance(f, models.CharField):
try: try:
max_length = int(f.max_length) max_length = int(f.max_length)
Expand Down
7 changes: 6 additions & 1 deletion tests/modeltests/invalid_models/invalid_models/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -220,14 +220,18 @@ class InvalidSetDefault(models.Model):
fk = models.ForeignKey('self', on_delete=models.SET_DEFAULT) fk = models.ForeignKey('self', on_delete=models.SET_DEFAULT)


class UnicodeForeignKeys(models.Model): class UnicodeForeignKeys(models.Model):
"""Foreign keys which can translate to ascii should be OK, but fail if they're not.""" """Foreign keys which can translate to ascii should be OK, but fail if
they're not."""
good = models.ForeignKey(u'FKTarget') good = models.ForeignKey(u'FKTarget')
also_good = models.ManyToManyField(u'FKTarget', related_name='unicode2') also_good = models.ManyToManyField(u'FKTarget', related_name='unicode2')


# In Python 3 this should become legal, but currently causes unicode errors # In Python 3 this should become legal, but currently causes unicode errors
# when adding the errors in core/management/validation.py # when adding the errors in core/management/validation.py
#bad = models.ForeignKey(u'★') #bad = models.ForeignKey(u'★')


class PrimaryKeyNull(models.Model):
my_pk_field = models.IntegerField(primary_key=True, null=True)



model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer. model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer.
invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer. invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer.
Expand Down Expand Up @@ -338,4 +342,5 @@ class UnicodeForeignKeys(models.Model):
invalid_models.nonexistingorderingwithsingleunderscore: "ordering" refers to "does_not_exist", a field that doesn't exist. invalid_models.nonexistingorderingwithsingleunderscore: "ordering" refers to "does_not_exist", a field that doesn't exist.
invalid_models.invalidsetnull: 'fk' specifies on_delete=SET_NULL, but cannot be null. invalid_models.invalidsetnull: 'fk' specifies on_delete=SET_NULL, but cannot be null.
invalid_models.invalidsetdefault: 'fk' specifies on_delete=SET_DEFAULT, but has no default value. invalid_models.invalidsetdefault: 'fk' specifies on_delete=SET_DEFAULT, but has no default value.
invalid_models.primarykeynull: "my_pk_field": Primary key fields cannot have null=True.
""" """

0 comments on commit 75ddbf7

Please sign in to comment.