sqldiff: Exception with descending indizes
Author: stefan6419846Created Jan 16, 2023Updated Jun 2, 2026
Using a model which has a descending index throws an exception:
stefan@localhost:~/project$ manage.py sqldiff --all-applications --include-proxy-models
Traceback (most recent call last):
File "/home/stefan/lib/python3.6/site-packages/django/db/models/options.py", line 565, in get_field
return self.fields_map[field_name]
KeyError: '-datetime'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/stefan/project/bin/manage.py", line 42, in <module>
main()
File "/home/stefan/project/bin/manage.py", line 33, in main
execute_from_command_line()
File "/home/stefan/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/stefan/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 1402, in run_from_argv
super().run_from_argv(argv)
File "/home/stefan/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 1389, in execute
super().execute(*args, **options)
File "/home/stefan/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/utils.py", line 59, in inner
ret = func(self, *args, **kwargs)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 1382, in handle
sqldiff_instance.find_differences()
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 680, in find_differences
self.find_index_missing_in_model(meta, table_indexes, table_constraints, table_name)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 499, in find_index_missing_in_model
index_together = self.get_index_together(meta)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 391, in get_index_together
return self.expand_together(indexes_normalized, meta)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 406, in expand_together
tuple(meta.get_field(field).attname for field in fields)
File "/home/stefan/lib/python3.6/site-packages/django_extensions/management/commands/sqldiff.py", line 406, in <genexpr>
tuple(meta.get_field(field).attname for field in fields)
File "/home/stefan/lib/python3.6/site-packages/django/db/models/options.py", line 567, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: MyModel has no field named '-datetime'Example definition:
class MyModel(models.Model):
datetime = models.DateTimeField(default=datetime.datetime.now)
class Meta:
indexes = [
models.Index(fields=['-datetime', ]),
]Source: django-extensions/django-extensions