我在Django v1.6.2上运行unittest并收到此错误:
ValueError: Can't do subqueries with queries on different DBs.
但在运行网站时却没有发生。
我只指定了一个数据库。
快速搜索谷歌只找到一个相关的结果,这对我没有帮助。
更新详情:
这是管理M2M关系的中间模型,这是导致问题的查询:
ProductCategory.objects.filter(category=category, product=product).delete()
更新:添加了跟踪
======================================================================
ERROR [0.079s]: test_sync (goproducts.apps.products.tests.productSyncTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/private/var/www/goproducts/goproducts/goproducts/apps/products/tests.py", line 55, in test_sync
product.objects.sync(self.message)
File "/private/var/www/goproducts/goproducts/goproducts/apps/products/managers.py", line 97, in sync
ProductCategory.objects.filter(category=category, product=product).delete()
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/query.py", line 464, in delete
collector.collect(del_query)
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/deletion.py", line 166, in collect
reverse_dependency=reverse_dependency)
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/deletion.py", line 77, in add
if not objs:
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/query.py", line 100, in __nonzero__
self._fetch_all()
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
self._result_cache = list(self.iterator())
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 709, in results_iter
for rows in self.execute_sql(MULTI):
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 772, in execute_sql
sql, params = self.as_sql()
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 94, in as_sql
having, h_params = self.query.having.as_sql(qn=qn, connection=self.connection)
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/sql/where.py", line 103, in as_sql
sql, params = child.as_sql(qn=qn, connection=connection)
File "/private/var/www/goproducts/lib/python2.7/site-packages/django/db/models/sql/where.py", line 401, in as_sql
raise ValueError("Can't do subqueries with queries on different DBs.")
ValueError: Can't do subqueries with queries on different DBs.
----------------------------------------------------------------------
答案 0 :(得分:-1)
我发现了问题,我使用模拟库来模拟同步功能
Category.objects.sync = MagicMock()
对此进行了评论,修复了它。
PS,我确保我的测试没有被破坏,因为我删除了模拟。