Search code examples
djangopostgresqldjango-mptt

django-mptt different data ordering on two machines with same code and data


I'm using django-mptt library for creating directory hierarchy like this:

directory_a
   directory_some_name
       directory_a
       directory_b
   directory_some_name
directory_b
   directory_some_name
   directory_some_name

models.py

class BaseDirectory(MPTTModel):
    name = models.CharField(_('Name'), max_length=120)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True,
                            blank=True, related_name='children',
                            verbose_name=_('parent directory'))

    def __str__(self):
        return self.name

    def clean(self):
        # some cleaning
        super().clean()

    def save(self, *args, **kwargs):
        self.clean()
        super().save(*args, **kwargs)

    class MPTTMeta:
        order_insertion_by = ['name']

    class Meta:
        abstract = True


class OtherDirectory(BaseDirectory):
    # directory model from another application
    class Meta:
        verbose_name = _('directory')
        verbose_name_plural = _('directories')

I deployed this code to server and it works fine(all items inserting in alphabetic order, if i change it somehow and run OtherDirectory.objects.rebuild() it returns back to normal alphabetic order. But locally on my machine alphabetic ordering didn't work. It's randomly puts new element into parent tree.

I'am tried to download database backup from my server and restore it locally, but if i run OtherDirectory.objects.rebuild() data again shuffled in wrong order.

After some researching of django-mptt sources and debugging my app i found out this queryset in django mptt contains different data between my local machine and server with same db data and code, filters and order_by:

queryset = node.__class__._tree_manager.db_manager(
                node._state.db).filter(filters).order_by(*order_by)

And now i'm stuck. Why this can be?

local machine: OS X Mojave. DB: postgresql 9.6.10 with postgis 2.5.2, python3.7.3, django 2.1.7, django-mptt 0.9.1

server: Ubuntu, postgresql 9.6.10 with postgis 2.5.1 python3.7.1, django 2.1.7, django-mptt 0.9.1


Solution

  • Find out solution. The problem was in postgresql ordering by Russian words on OS X. Fix with following steps in this article(in Russian) .