Do null foreign keys slow things down?

I'm implementing file attachments for certain objects in the project I am working on. There are six or so object classes which might reasonably have attached files (which would be revealed in their Detail views and managed via a link from there). The model would be like

class JobFile( models.Model):
    
    job = models.ForeignKey( 'jobs.Job', models.SET_NULL,
                            null=True, blank=True, related_name='attachments', )
    quote = models.ForeignKey( 'quotation.Quote', models.SET_NULL, 
                            null=True, blank=True, related_name='attachments', )
    #etc

    document = models.FileField( ... )  # the attachment

One advantage of this rather than a Generic ForeignKey is that an upload can be attached to more than one sort of object at once. Another is the simplicity of referring to obj.attachments.all() in the obj detail views. I'm not looking for a large set of object classes to which these files might be attached.

However, for any one file attachment most of its ForeignKeys will be null. I have seen various references to null ForeignKeys causing slow Django ORM queries. Is this anything I need to be concerned about?

If it makes any difference, these objects will be almost exclusively accessed via the attachments reverse ForeignKey manager on the related object. The only time I can see a need for explicit filtering of JobLine.objects.filter(field__isnull=True) is in a management context looking for "orphaned" files (which shouldn't normally happen).

Back to Top