一个有趣的问题。 获取未知列异常 - 请找到堆栈跟踪
我尝试获得新的潜在客户列表并回复潜在客户。我合并他们。当我合并它们时有一个例外。
调试后发现new_leads方法已经排除了两个字段的收集和传递。如果我们做到一个排除一切都很好。我的意思是不检查另一个,如果我们包含两个过滤器我们有问题。
我尝试使用过滤器/排除等,但它没有用。
查询集包含以下方法
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() <= 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))
# <== 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)
堆栈追踪: -
>>> 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 "<console>", line 1, in <module>
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'")
>>>
我正在使用MySqlDB
应用所有迁移并且Db处于正确状态
更新
问题与在创建Having子句时在select语句中可用的MYSQL madating列有关。参考 - Unknown column in 'having clause'。 在这种情况下,Django不会添加为要选择的列。因此错误。 我需要找一些方法在select子句中添加其他参数。
答案 0 :(得分:4)
当您未进行或已应用迁移时,通常会弹出此消息。具体来说,当您在Django(或任何ORM)中修改字段中的任何模型时,您需要通知SQL服务器,以便它可以在其表中反映它。现代Django通过一系列迁移来实现这一点,因此,如果您在项目生命周期中的任何时间拥有数据,则只需向前或向后运行迁移,就可以在历史中的任何时间运行代码。
长话短说,MySQL声称sub_region
字段不存在。您需要同步表格以反映您的模型。
有两个步骤,即在您的服务器上进行迁移并运行它们,如下所示为locations
应用。 在运行第二个命令之前进行备份,特别是在MySQL或SQLite上!
$ python manage.py makemigrations locations
$ python manage.py migrate locations
这将导致数据库服务器创建列,您不应再获得OperationalError。
答案 1 :(得分:1)
我找到了答案。
这不是迁移问题。
当拥有HAVING子句时,MYSQL要求这些参数被选中,如果没有被选中,它将引发异常,
因此我不得不强迫Django通过
包含两个字段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"))
这解决了这个问题。
答案 2 :(得分:0)
我知道问这个问题已经快3年了,但这是我的2美分。 由于我的表没有任何数据,因此我将其删除并在mysql上重新创建,然后将其从django中删除。最后,我在Django上重新创建了此程序,以解决错误。