#10244: FileFields can't be set to NULL in the db
-------------------------------------+-------------------------------------
     Reporter:  oyvind               |                    Owner:  Ondrej
                                     |  Pudiš
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  1.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  filefield NULL       |             Triage Stage:  Accepted
  empty                              |
    Has patch:  0                    |      Needs documentation:  1
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Ondrej Pudiš):

 * owner:  nobody => Ondrej Pudiš
 * status:  new => assigned
 * has_patch:  1 => 0
 * needs_tests:  1 => 0


Comment:

 As I understand the issue, if the file field is specified as nullable, it
 should accept `None` and in that case, store `NULL` on the database level
 instead of an empty string. In fact, it shouldn't even create an instance
 of `FieldFile` when creating a new `NullableDocument` object.

 The tests listed below are broken now but the patch should fix all of them
 giving a solution to all the issues enumerated in the comment above.

 {{{
 def test_create_empty(self):
     # when I create a new document with no file provided
     d = NullableDocument.objects.create(myfile=None)
     # I expect that the attribute itself is None
     self.assertIs(d.myfile, None)
     # I expect that I can filter the documents and find the one
     query = NullableDocument.objects.filter(myfile__isnull=True).exists()
     self.assertTrue(query)
     # I expect that the object remains None even after refreshing from the
 DB
     d.refresh_from_db()
     self.assertIs(d.myfile, None)

 def test_create_empty_multiple(self):
     # when the files are expected to be unique but nullable, I expect that
 I'm
     # allowed to create multiple records
     NullableDocument.objects.create(myfile=None)
     NullableDocument.objects.create(myfile=None)
     # and both are created
     query = NullableDocument.objects.filter(myfile__isnull=True).count()
     self.assertEqual(query, 2)

 def test_create_empty_on_not_null(self):
     # when I try to store an empty file on non-nullable model, I expect to
 get an
     # integrity error
     with self.assertRaises(IntegrityError):
         Document.objects.create(myfile=None)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/10244#comment:20>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701836f8500b5-5cf902f8-b912-4de0-afb2-df6fcb1b0d1d-000000%40eu-central-1.amazonses.com.

Reply via email to