title: django.db.utils.OperationalError: (1054, "Unknown column
body: <p>An interesting issue. 
  Getting Unknown column exception -- Please find the stack trace</p>
  
  <p>I try to get new leads list and responded leads. I merge them. When I merge them there is an exception. </p>
  
  <p>After debugging its found that new_leads method has exclude of two fields collection and delivery . If we make it one exclude all is well . I mean dont check the other, if we include both the filters we have an issue. </p>
  
  <p>I tried using filter/ exclude etc. but it didnt work.</p>
  
  <p>Query Set contains following method </p>
  
  <pre><code>    def all_leads_related_to_user(self, user):
      """ User new and past leads
      Use this queryset for performing lead search.
      """
      new_leads = self.new_leads_for_user(user)
      responded_leads = self.leads_responded_by_user(user)
      all_leads = (new_leads | responded_leads).distinct() &lt;= Issue is here.
      return all_leads
  
      def new_leads_for_user(self, user):
       ....
  
      # User's location filter
      if user.sub_region_excluded_list:
          sub_region_exclude_list = [10, 12]
          qs = qs.exclude( Q(collection_point__sub_region_id__in=sub_region_exclude_list) |
                          Q(delivery_point__sub_region_id__in=sub_region_exclude_list)) 
  
                     # &lt;== Make it just one exclude it works.
  
  
  Model 
  
  class Suburb(models.Model):
      state = models.ForeignKey(State, blank=False)
      sub_region = models.ForeignKey(SubRegion, blank=False)
      postcode = models.CharField(_('postcode'), blank=False, max_length=10)
      name = models.CharField(_('suburb name'), blank=False, max_length=200)
  
  class Load(models.Model):
  .....
  
  collection_point = models.ForeignKey(Suburb, related_name='collection_point', on_delete=models.SET_NULL, null=True)
  delivery_point = models.ForeignKey(Suburb, related_name='delivery_point', on_delete=models.SET_NULL, null=True)
  </code></pre>
  
  <p>Stack Trace:- </p>
  
  <pre><code>    &gt;&gt;&gt; Load.objects.all_leads_related_to_user(User.objects.all()[0])
  Load.objects.all_leads_related_to_user(User.objects.all()[0])
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/models/query.py", line 226, in __repr__
      data = list(self[:REPR_OUTPUT_SIZE + 1])
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/models/query.py", line 250, in __iter__
      self._fetch_all()
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/models/query.py", line 1103, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/models/query.py", line 53, in __iter__
      results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 886, in execute_sql
      raise original_exception
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 876, in execute_sql
      cursor.execute(sql, params)
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 80, in execute
      return super(CursorDebugWrapper, self).execute(sql, params)
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
      return self.cursor.execute(sql, params)
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
      six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "/data/fq/venv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
      raise value.with_traceback(tb)
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute
      return self.cursor.execute(sql, params)
    File "/data/fq/venv/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 101, in execute
      return self.cursor.execute(query, args)
    File "/data/fq/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 250, in execute
      self.errorhandler(self, exc, value)
    File "/data/fq/venv/lib/python3.4/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
      raise errorvalue
    File "/data/fq/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 247, in execute
      res = self._query(query)
    File "/data/fq/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 411, in _query
      rowcount = self._do_query(q)
    File "/data/fq/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 374, in _do_query
      db.query(q)
    File "/data/fq/venv/lib/python3.4/site-packages/MySQLdb/connections.py", line 292, in query
      _mysql.connection.query(self, query)
  django.db.utils.OperationalError: (1054, "Unknown column 'locations_suburb.sub_region_id' in 'having clause'")
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>I'm using MySqlDB</p>
  
  <h2>Note:-</h2>
  
  <p>All migrations are applied and Db is in right state</p>
  
  <p><strong>Update</strong></p>
  
  <p>Issue is related to MYSQL madating columns to be available in select statements when its creating a Having clause . Refer to -
   <a href="https://stackoverflow.com/questions/36105812/unknown-column-in-having-clause">Unknown column in &#39;having clause&#39;</a>. 
  Django in this instance is not adding as the column to be selected. hence the error. 
   I need to some how find a way to add this in the select clause with other params.</p>