Repository: learningequality/kolibri

Issue:
OperationalError: too many SQL variables
## Summary

Happens in VM and my local dev repo, where I have imported CK-12 channel from USB drive. Channel isn't showing as `Featured in...` in `Recommended` tab with the below errors in the console, but its content can be browsed correctly in `Topics`.

![fullscreen_10_10_17_03_59](https://user-images.githubusercontent.com/1457929/31458243-cf9f4206-aebf-11e7-8c00-7ad4059b2270.png)

![fullscreen_10_10_17_03_54](https://user-images.githubusercontent.com/1457929/31458346-1aef27ee-aec0-11e7-98ac-47e6a342a3b1.png)

## System information

 - Version: latest develop
 - Operating system: Win VM and develop on OSX
 - Browser: all

## Traceback or relevant snippet from server.log or browser console


```
Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/api/contentnode/all_content/?channel_id=1d8f6d84618153c18c695d85074952a7&1507745570490=1507745570490

Django Version: 1.9.7
Python Version: 2.7.14
Installed Applications:
[u'kolibri.core',
 u'django.contrib.admin',
 u'django.contrib.auth',
 u'django.contrib.contenttypes',
 u'django.contrib.sessions',
 u'django.contrib.messages',
 u'django.contrib.staticfiles',
 u'kolibri.auth.apps.KolibriAuthConfig',
 u'kolibri.content',
 u'kolibri.logger',
 u'kolibri.tasks.apps.KolibriTasksConfig',
 u'kolibri.core.webpack',
 u'kolibri.core.exams',
 u'kolibri.core.device',
 u'kolibri.core.discovery',
 u'rest_framework',
 u'django_js_reverse',
 u'jsonfield',
 u'morango',
 u'kolibri.plugins.management',
 u'kolibri.plugins.learn',
 u'kolibri.plugins.document_pdf_render',
 u'kolibri.plugins.html5_app_renderer',
 u'kolibri.plugins.media_player',
 u'kolibri.plugins.setup_wizard',
 u'kolibri.plugins.coach',
 u'kolibri.plugins.user',
 u'kolibri.plugins.style_guide',
 u'kolibri_exercise_perseus_plugin']
Installed Middleware:
(u'django.contrib.sessions.middleware.SessionMiddleware',
 u'kolibri.core.device.middleware.KolibriLocaleMiddleware',
 u'django.middleware.common.CommonMiddleware',
 u'django.middleware.csrf.CsrfViewMiddleware',
 u'kolibri.plugins.setup_wizard.middleware.SetupWizardMiddleware',
 u'kolibri.auth.middleware.CustomAuthenticationMiddleware',
 u'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 u'django.contrib.messages.middleware.MessageMiddleware',
 u'django.middleware.clickjacking.XFrameOptionsMiddleware',
 u'django.middleware.security.SecurityMiddleware')



Traceback:

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
  87.             return self.dispatch(request, *args, **kwargs)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)

File "kolibri/content/api.py" in all_content
  280.         return Response(serializer.data)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/rest_framework/serializers.py" in data
  674.         ret = super(ListSerializer, self).data

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/rest_framework/serializers.py" in data
  239.                 self._data = self.to_representation(self.instance)

File "kolibri/content/serializers.py" in to_representation
  170.         if not data:

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in __nonzero__
  266.         return type(self).__bool__(self)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in __bool__
  262.         self._fetch_all()

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1076.             self._prefetch_related_objects()

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in _prefetch_related_objects
  656.         prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in prefetch_related_objects
  1457.                 obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in prefetch_one_level
  1556.         prefetcher.get_prefetch_queryset(instances, lookup.get_current_queryset(level)))

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py" in get_prefetch_queryset
  543.             for rel_obj in queryset:

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  258.         self._fetch_all()

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in _fetch_all
  1074.             self._result_cache = list(self.iterator())

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/query.py" in __iter__
  52.         results = compiler.execute_sql()

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  848.             cursor.execute(sql, params)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/utils.py" in __exit__
  95.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Users/mac/.venvs/kolibri-sans2/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py" in execute
  323.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /api/contentnode/all_content/
Exception Value: too many SQL variables
```

## How to reproduce

1. [Download ZIP](https://drive.google.com/file/d/0B3ZvXdQEq36jM0lJSm96OUNmMjA/view?usp=sharing) (beware, 6GB)
1. Unzip and do a local import 
1. Go to `Learn`




Discussion:
@lyw07 Did you try importing this channel (this is on the link from Slack)? Could you reproduce this error?
@radinamatic was this on latest develop, or on @lyw07's PR? Just want to verify because they may not be completely identical.

If the latter, please double check and replicate on latest develop also.

Resolving patch:
diff --git a/kolibri/content/api.py b/kolibri/content/api.py
--- a/kolibri/content/api.py
+++ b/kolibri/content/api.py
@@ -274,7 +274,7 @@ def next_content(self, request, **kwargs):
 
     @list_route(methods=['get'])
     def all_content(self, request, **kwargs):
-        queryset = self.filter_queryset(self.get_queryset()).exclude(kind=content_kinds.TOPIC)
+        queryset = self.filter_queryset(self.get_queryset(prefetch=False)).exclude(kind=content_kinds.TOPIC)
 
         serializer = self.get_serializer(queryset, many=True, limit=24)
         return Response(serializer.data)


Test patch:
diff --git a/kolibri/content/test/test_content_app.py b/kolibri/content/test/test_content_app.py
--- a/kolibri/content/test/test_content_app.py
+++ b/kolibri/content/test/test_content_app.py
@@ -174,6 +174,11 @@ def test_contentnode_recommendations(self):
         response = self.client.get(self._reverse_channel_url("contentnode-list"), data={"recommendations_for": id})
         self.assertEqual(len(response.data), 2)
 
+    def test_contentnode_allcontent(self):
+        nodes = content.ContentNode.objects.exclude(kind=content_kinds.TOPIC).count()
+        response = self.client.get(self._reverse_channel_url("contentnode-all-content"))
+        self.assertEqual(len(response.data), nodes)
+
     def test_channelmetadata_list(self):
         response = self.client.get(reverse("channel-list", kwargs={}))
         self.assertEqual(response.data[0]['name'], 'testing')