Ticket #15884: 15884.diff

File 15884.diff, 2.7 KB (added by jpichon, 4 years ago)
  • django/core/management/validation.py

    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): 
    4040                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)
    4141            if f.name.endswith('_'):
    4242                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)
    4345            if isinstance(f, models.CharField):
    4446                try:
    4547                    max_length = int(f.max_length)
  • tests/modeltests/invalid_models/models.py

    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): 
    234234   tags = generic.GenericRelation(TaggedObject)
    235235   user_tags = generic.GenericRelation(UserTaggedObject)
     237class PrimaryKeyNull(models.Model):
     238    my_pk_field = models.IntegerField(primary_key=True, null=True)
    237240model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer.
    238241invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer.
    239242invalid_models.fielderrors: "charfield3": CharFields require a "max_length" attribute that is a positive integer.
    invalid_models.nonexistingorderingwithsingleunderscore: "ordering" refers to "do 
    344347invalid_models.invalidsetnull: 'fk' specifies on_delete=SET_NULL, but cannot be null.
    345348invalid_models.invalidsetdefault: 'fk' specifies on_delete=SET_DEFAULT, but has no default value.
    346349invalid_models.articleattachment: Model 'UserTaggedObject' must have a GenericForeignKey in order to create a GenericRelation that points to it.
     350invalid_models.primarykeynull: "my_pk_field": Primary key fields do not accept null values.
Back to Top