Opened 10 years ago
Closed 10 years ago
#23706 closed Bug (needsinfo)
Accessing related object of object from not default DB, queries default DB
Reported by: | Deroky | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.6 |
Severity: | Normal | Keywords: | |
Cc: | Deroky | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When having multiple databases configured (like below), with different database models (apps migrated) in each (let's say you have the app CustomAuth in default and the app International in clientdb (models sampled below), if you get a Client and then try to retrieve its related object you get this error (Django is trying to resolve the get using default db instead of the db of the instance that you have):
>>> c=Client.objects.using('clientdb').get(pk=1) <Client: Deroky> >>> c.__dict__['_state'].__dict__ {'adding': False, 'db': 'clientdb'} >>> c.currency Traceback (most recent call last): File "<console>", line 1, in <module> File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 320, in __get__ rel_obj = qs.get() File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/models/query.py", line 301, in get num = len(clone) File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__ self._fetch_all() File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all self._result_cache = list(self.iterator()) File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator for row in compiler.results_iter(): File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter for rows in self.execute_sql(MULTI): File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql cursor.execute(sql, params) File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/backends/util.py", line 69, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute return self.cursor.execute(sql, params) File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/opt/env/ooint_platform/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute return self.cursor.execute(sql, params) ProgrammingError: relation "international_currency" does not exist LINE 1: ...y"."id", "international_currency"."currency" FROM "internati...
Config:
DATABASES = { 'default': { 'ENGINE':'django.db.backends.postgresql_psycopg2', 'NAME':'centraldb', 'USER':'admin', 'PASSWORD':'', 'PORT':5432, 'HOST':'sql-node1', }, 'clientdb': { 'ENGINE':'django.db.backends.postgresql_psycopg2', 'NAME':'clientdb', 'USER':'admin', 'PASSWORD':'', 'PORT':5432, 'HOST':'sql-node1', } }
App CustomAuth models:
(not relevant, but have no Currency model)
App International models:
class Client(models.Model): client = models.CharField(max_length=52,unique=True,verbose_name=_('Client')) currency = models.ForeignKey(Currency,verbose_name = _('From Currency')) def __unicode__(self): return self.client class Currency(models.Model): currency = models.CharField(unique=True,max_length=255,verbose_name='Currency') def __unicode__(self): return self.currency
Change History (4)
comment:1 by , 10 years ago
Summary: | Accessing related object from not default DB, queries default DB → Accessing related object of object from not default DB, queries default DB |
---|
comment:2 by , 10 years ago
Cc: | added |
---|
comment:3 by , 10 years ago
comment:4 by , 10 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
I don't see how your example is different from an existing test in Django's test suite? Could you write a failing test there to reproduce your issue?