Repository: learningequality/kolibri

Issue:
Unable to see lessons created after upgrading Kolibri with PostgreSQL
<!--
Instructions:
 * Fill out the sections below, replace …'s with information about your issue
 * Use the 'preview' function above this text box to verify formatting before submitting
-->

### Observed behavior
<!--
Description of the behavior that was observed, including screenshots or other references when applicable
-->

After upgrading Kolibri from 0.13.3 to 0.14.0b8, I'm unable to see the lessons, quizzes, class activities, and reports that I created before the upgrade on the Coach page. This issue only occurs when running Kolibri in PostgreSQL. In sqlite database, everything works fine.

### Expected behavior
<!--
Description of what behavior was expected but did not occur
-->

Users should be able to see lessons/quizzes they created previously after the version upgrade 

### User-facing consequences
<!--
Implications and real-world consequences for learners, coaches, admins, and other users of the application
-->

Users are unable to see the lessons and quizzes, although they exist in the database

### Errors and logs
<!--
Relevant logs from:
 * the command line
 * ~/.kolibri/logs/kolibri.txt
 * the browser console

Please wrap errors in triple backticks for clean formatting like this:
```
01:10 info: something happened
01:12 error: something bad happened
```
-->

Here's the error in the console:
<img width="1280" alt="Screen Shot 2020-07-08 at 2 08 15 PM" src="https://user-images.githubusercontent.com/19424916/86973443-6a550c80-c129-11ea-82ae-5a85c89802aa.png">

coach page after the upgrade:
<img width="1278" alt="Screen Shot 2020-07-08 at 2 27 29 PM" src="https://user-images.githubusercontent.com/19424916/86973539-9ec8c880-c129-11ea-9993-ba32efe90826.png">


### Steps to reproduce
<!--
Precise steps that someone else can follow in order to see this behavior
-->

1. start kolibri 0.13.3 in postgresql
2. create a class with a lesson and some class activity
3. upgrade kolibri to 0.14.0b8
4. check the coach page again

### Context
<!--
Tell us about your environment, including:
 * Kolibri version
 * Operating system
 * Browser
-->

Kolibri version: 0.14.0b8

Unable to see lessons created after upgrading Kolibri with PostgreSQL
<!--
Instructions:
 * Fill out the sections below, replace …'s with information about your issue
 * Use the 'preview' function above this text box to verify formatting before submitting
-->

### Observed behavior
<!--
Description of the behavior that was observed, including screenshots or other references when applicable
-->

After upgrading Kolibri from 0.13.3 to 0.14.0b8, I'm unable to see the lessons, quizzes, class activities, and reports that I created before the upgrade on the Coach page. This issue only occurs when running Kolibri in PostgreSQL. In sqlite database, everything works fine.

### Expected behavior
<!--
Description of what behavior was expected but did not occur
-->

Users should be able to see lessons/quizzes they created previously after the version upgrade 

### User-facing consequences
<!--
Implications and real-world consequences for learners, coaches, admins, and other users of the application
-->

Users are unable to see the lessons and quizzes, although they exist in the database

### Errors and logs
<!--
Relevant logs from:
 * the command line
 * ~/.kolibri/logs/kolibri.txt
 * the browser console

Please wrap errors in triple backticks for clean formatting like this:
```
01:10 info: something happened
01:12 error: something bad happened
```
-->

Here's the error in the console:
<img width="1280" alt="Screen Shot 2020-07-08 at 2 08 15 PM" src="https://user-images.githubusercontent.com/19424916/86973443-6a550c80-c129-11ea-82ae-5a85c89802aa.png">

coach page after the upgrade:
<img width="1278" alt="Screen Shot 2020-07-08 at 2 27 29 PM" src="https://user-images.githubusercontent.com/19424916/86973539-9ec8c880-c129-11ea-9993-ba32efe90826.png">


### Steps to reproduce
<!--
Precise steps that someone else can follow in order to see this behavior
-->

1. start kolibri 0.13.3 in postgresql
2. create a class with a lesson and some class activity
3. upgrade kolibri to 0.14.0b8
4. check the coach page again

### Context
<!--
Tell us about your environment, including:
 * Kolibri version
 * Operating system
 * Browser
-->

Kolibri version: 0.14.0b8



Discussion:
This might be related to #7200 and #7214, which is basically a serialization issue for `ValueViewsets` returning data from postgres. In those issues, it was a different array of IDs that was not being serialized properly. There might be something similar going on with the `member_ids` array, in a different place. A quick search of the code says this `member_ids` field comes from the ClassSummary API
Also means we need test coverage of more endpoints to catch this!
Rolling back to 0.13.3 using the same database makes everything visible again.

Could this be related? we get this running kolibri commands.

/usr/lib/python3/dist-packages/kolibri/dist/tzlocal/unix.py:108: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  while start is not 0:
/usr/lib/python3/dist-packages/kolibri/dist/rest_framework/utils/serializer_helpers.py:107: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if value is None or value is '':

Hrm, that seems like something slightly off inside some of our dependencies but is probably not the issue here - thanks for the extra context, @cyberorg!
This might be related to #7200 and #7214, which is basically a serialization issue for `ValueViewsets` returning data from postgres. In those issues, it was a different array of IDs that was not being serialized properly. There might be something similar going on with the `member_ids` array, in a different place. A quick search of the code says this `member_ids` field comes from the ClassSummary API
Also means we need test coverage of more endpoints to catch this!
Rolling back to 0.13.3 using the same database makes everything visible again.

Could this be related? we get this running kolibri commands.

/usr/lib/python3/dist-packages/kolibri/dist/tzlocal/unix.py:108: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  while start is not 0:
/usr/lib/python3/dist-packages/kolibri/dist/rest_framework/utils/serializer_helpers.py:107: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if value is None or value is '':

Hrm, that seems like something slightly off inside some of our dependencies but is probably not the issue here - thanks for the extra context, @cyberorg!

Resolving patch:
diff --git a/kolibri/core/query.py b/kolibri/core/query.py
--- a/kolibri/core/query.py
+++ b/kolibri/core/query.py
@@ -71,7 +71,10 @@ def get_source_field(model, field_path):
     paths = field_path.split("__")
     while len(paths) > 1:
         model = model._meta.get_field(paths.pop(0)).related_model
-    return model._meta.get_field(paths[0])
+    field = model._meta.get_field(paths[0])
+    if field.is_relation and field.foreign_related_fields:
+        field = field.foreign_related_fields[0]
+    return field
 
 
 def annotate_array_aggregate(queryset, **kwargs):


Test patch: