django.db.utils.OperationalError: (1054, "Unknown column

Solution 1:

This usually pops up when you haven't made or applied migrations. Specifically, when you modify the fields any model in Django (or any ORM), you need to inform the SQL server so it can reflect it in its tables. Modern Django implements this by a series of migrations, so that if you have data from any time in the life of your project, you can run it on code from any time in history by simply running the migrations forwards or backwards.

Long story short, MySQL claims the sub_region field doesn't exist. You need to sync the tables to reflect your models.

There are two steps, making the migrations and running them on your server, shown below for your locations app. Take a backup before running the second command, especially on MySQL or SQLite!

$ python manage.py makemigrations locations
$ python manage.py migrate locations 

This will cause the database server to create the column, and you should no longer get an OperationalError.

Solution 2:

It is not a migration issue.

When having a HAVING Clause MYSQL mandates those parameters to be in selected, if not selected it will throw an exception. Therefore I had to force Django to include both the fields by:

qs = qs.annotate(collection_point__sub_region_id = F("collection_point__sub_region_id"),
                 delivery_point__sub_region_id = F("delivery_point__sub_region_id"))