[Django] #23706: Accessing related object from not default DB, queries default DB

10 views
Skip to first unread message

Django

unread,
Oct 23, 2014, 12:36:46 PM10/23/14
to django-...@googlegroups.com
#23706: Accessing related object from not default DB, queries default DB
----------------------------------------------+--------------------
Reporter: Deroky | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23706>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 23, 2014, 12:37:24 PM10/23/14
to django-...@googlegroups.com
#23706: Accessing related object of object from not default DB, queries default DB
-------------------------------------+-------------------------------------

Reporter: Deroky | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.6
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Deroky):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/23706#comment:1>

Django

unread,
Oct 27, 2014, 7:57:01 AM10/27/14
to django-...@googlegroups.com
#23706: Accessing related object of object from not default DB, queries default DB
-------------------------------------+-------------------------------------

Reporter: Deroky | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.6
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Deroky):

* cc: Deroky (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/23706#comment:2>

Django

unread,
Oct 27, 2014, 6:32:24 PM10/27/14
to django-...@googlegroups.com
#23706: Accessing related object of object from not default DB, queries default DB
-------------------------------------+-------------------------------------

Reporter: Deroky | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.6
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

I don't see how your example is different from
[https://github.com/django/django/blob/e460b1c573359b256d79c0d4cf6aeb492deca97c/tests/multiple_database/tests.py#L400-L401
an existing test in Django's test suite]? Could you write a failing test
there to reproduce your issue?

--
Ticket URL: <https://code.djangoproject.com/ticket/23706#comment:3>

Django

unread,
Oct 30, 2014, 10:52:15 AM10/30/14
to django-...@googlegroups.com
#23706: Accessing related object of object from not default DB, queries default DB
-------------------------------------+-------------------------------------
Reporter: Deroky | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.6
(models, ORM) | Resolution: needsinfo

Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => needsinfo


--
Ticket URL: <https://code.djangoproject.com/ticket/23706#comment:4>

Reply all
Reply to author
Forward
0 new messages