v1.3.2: getting "Unknown keyword: ORDER BY" trying to fetch the first document in. a collection
One line description of the issue
I just generated from scratch a simple Django 3.0.6 app with Djongo 1.3.2.
In settings.py DATABASES I mapped to the MongoDB database that is on a different host, and created a app/models.py file with a single model for one of the collection.
When using the model I get the error Unknown keyword: ORDER BY.
Python script
models.py
from djongo import models
class Campaign(models.Model):
_id = models.ObjectIdField(primary_key=True)
name = models.CharField()
objects = models.DjongoManager()
def __str__(self):
return str(self._id)
class Meta:
db_table = "campaigns"
app_label = "model_campaign"
campaign_view.py
from django.http import HttpResponse
from django.views.generic import View
from app.models import Campaign
class CampaignsView(View):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def get(self, request):
record = Campaign.objects.first()
return HttpResponse(record)
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles'
]
DATABASES = {
"default": {
"NAME": "mydb",
"ENGINE": "djongo",
"HOST": 'mongodb://10.0.xxx.xxx:27017/mydb'
}
}
DATABASE_ROUTERS = []
Traceback
Internal Server Error: /api/campaigns
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 842, in parse
return handler(self, statement)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 912, in _select
return SelectQuery(self.db, self.connection_properties, sm, self._params)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 115, in __init__
super().__init__(*args)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 61, in __init__
self.parse()
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 154, in parse
raise SQLDecodeError(f'Unknown keyword: {tok}')
djongo.exceptions.SQLDecodeError: Unknown keyword: ORDER BY
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/djongo/cursor.py", line 56, in execute
params)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 769, in __init__
self._query = self.parse()
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 864, in parse
raise exe from e
djongo.exceptions.SQLDecodeError: FAILED SQL: SELECT "campaigns"."_id", "campaigns"."name" FROM "campaigns" ORDER BY "campaigns"."_id" ASC LIMIT 1
Params: ()
Version: 1.3.2
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e
djongo.database.DatabaseError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/newrelic/hooks/framework_django.py", line 886, in wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/app/app/api/endpoints/campaigns_view.py", line 11, in get
record = Campaign.objects.first()
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 664, in first
for obj in (self if self.ordered else self.order_by('pk'))[:1]:
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 276, in __iter__
self._fetch_all()
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e
django.db.utils.DatabaseError
tried with v1.3.1 and I get this error:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 824, in parse
return handler(self, statement)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 963, in _select
self._query = SelectQuery(self.db, self.connection_properties, sm, self._params)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 111, in __init__
super().__init__(*args)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 72, in __init__
self.parse()
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 151, in parse
raise SQLDecodeError
djongo.sql2mongo.SQLDecodeError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/newrelic/hooks/framework_django.py", line 886, in wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/app/app/api/endpoints/campaigns_view.py", line 11, in get
record = Campaign.objects.first()
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 664, in first
for obj in (self if self.ordered else self.order_by('pk'))[:1]:
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 276, in __iter__
self._fetch_all()
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/djongo/cursor.py", line 53, in execute
params)
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 753, in __init__
self.parse()
File "/usr/local/lib/python3.7/site-packages/djongo/sql2mongo/query.py", line 846, in parse
raise exe from e
djongo.sql2mongo.SQLDecodeError: FAILED SQL: SELECT "campaigns"."_id", "campaigns"."name" FROM "campaigns" ORDER BY "campaigns"."_id" ASC LIMIT 1
Params: ()
Version: 1.3.1
same here
Same thing here. Made it work by using MaModel.objects.all()[0].
Got the same problem in V1.3.3. Found this warning information during pip installing other project requirements:
djongo 1.3.3 requires sqlparse==0.2.4, but you'll have sqlparse 0.4.1 which is incompatible.
So after
pip install sqlparse==0.2.4
problem fixed.
Have the same problem. We use dockerization to ship our backend and while scanning found an issue with sqlparse 0.2.4 so have to update to 0.4.4 to fix the high vulnerabilities. Is there any plan to make djongo compatible with latest version of sqlparse ?