def de_dupe_version_table(apps, schema_editor):
 db_alias = schema_editor.connection.alias
 Version = apps.get_model(u'reversion', u'Version')
 keep_version_ids = Version.objects.using(db_alias).order_by().values_list(u'revision_id', u'content_type_id', u'object_id').annotate(max_pk=models.Max(u'pk')).values_list(u'max_pk', flat=True)
 if (keep_version_ids.count() == Version.objects.using(db_alias).all().count()):
  return
 delete_version_ids = list(Version.objects.using(db_alias).exclude(pk__in=keep_version_ids).values_list(u'pk', flat=True))
 Version.objects.using(db_alias).filter(pk__in=delete_version_ids).delete()

def de_dupe_version_table ( apps , schema_editor ) : db_alias = schema_editor . connection . alias Version = apps . get_model ( u'reversion' , u'Version' ) keep_version_ids = Version . objects . using ( db_alias ) . order_by ( ) . values_list ( u'revision_id' , u'content_type_id' , u'object_id' ) . annotate ( max_pk = models . Max ( u'pk' ) ) . values_list ( u'max_pk' , flat = True ) if ( keep_version_ids . count ( ) == Version . objects . using ( db_alias ) . all ( ) . count ( ) ) : return delete_version_ids = list ( Version . objects . using ( db_alias ) . exclude ( pk__in = keep_version_ids ) . values_list ( u'pk' , flat = True ) ) Version . objects . using ( db_alias ) . filter ( pk__in = delete_version_ids ) . delete ( )
removes some duplicate version models that may have crept into the database and will prevent the unique index being added by migration 0004 .