How to fix the MultipleObjectsReturned error in Hue

How to fix the MultipleObjectsReturned error in Hue

When going on the Home page (/home) in Hue 3.0, this error could appear:

MultipleObjectsReturned: get() returned more than one DocumentPermission -- it returned 2! Lookup parameters were {'perms': 'read', 'doc': <Document: saved query Sample: Job loss sample>}

This is fixed in Hue 3.6 and here is a way to repair it:

1. Backup the Hue database

2. Run the cleanup script

from desktop.models import DocumentPermission, Document

for document in Document.objects.all():
    perm, created = DocumentPermission.objects.get_or_create(doc=document, perms=DocumentPermission.READ_PERM)
  except DocumentPermission.MultipleObjectsReturned, ex:
    # We can delete duplicate perms of a document
    dups = DocumentPermission.objects.filter(doc=document, perms=DocumentPermission.READ_PERM)
    perm = dups[0]
    for dup in dups[1:]:
      print 'Deleting duplicate %s' % dup