mongodb-labs / django-mongodb

MongoDB Backend for Django
Apache License 2.0
16 stars 7 forks source link

NotSupportedError: Ordering can't span tables on MongoDB (content_type__app_label) #34

Closed Jibola closed 4 months ago

Jibola commented 6 months ago

Steps to reproduce

Followed instructions for initial setup from #18. Created my first admin account. From there, I called python manage.py makemigrations which worked. Afterward I then called python manage.py migrate which led to this initial error.

python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying admin.0004_alter_logentry_id... OK
  Applying auth.0013_alter_group_id_alter_permission_id_alter_user_id... OK
  Applying contenttypes.0003_alter_contenttype_id... OK
Traceback (most recent call last):
  File "/Users/jib/mongo/starterapp/storefront/manage.py", line 22, in <module>
    main()
  File "/Users/jib/mongo/starterapp/storefront/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 383, in handle
    emit_post_migrate_signal(
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/core/management/sql.py", line 52, in emit_post_migrate_signal
    models.signals.post_migrate.send(
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/dispatch/dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/contrib/auth/management/__init__.py", line 91, in create_permissions
    all_perms = set(
                ^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/db/models/query.py", line 400, in __iter__
    self._fetch_all()
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django/db/models/query.py", line 1928, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django_mongodb/compiler.py", line 47, in results_iter
    results = self.build_query(columns).fetch()
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django_mongodb/compiler.py", line 140, in build_query
    query.order_by(self._get_ordering())
                   ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniforge/base/envs/django/lib/python3.12/site-packages/django_mongodb/compiler.py", line 170, in _get_ordering
    raise NotSupportedError("Ordering can't span tables on MongoDB (%s)." % order)
django.db.utils.NotSupportedError: Ordering can't span tables on MongoDB (content_type__app_label).
timgraham commented 6 months ago

We can't support ordering on related tables until we support joins. If we can't support joins (or until we can, if it'll be a longer-term goal), we could ignore ordering if it references a related table.

I temporarily worked around this in the Django fork by commenting out Permission.Meta.ordering.

Jibola commented 6 months ago

We can't support ordering on related tables until we support joins. If we can't support joins (or until we can, if it'll be a longer-term goal), we could ignore ordering if it references a related table.

I temporarily worked around this in the Django fork by commenting out Permission.Meta.ordering.

Good to note. I'd say we will have to put strong efforts into support joins via the $lookup operator. That or we need to unpack all of the underlying join operations associated with the builtin admin package.