#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.0c392262c4bae190a5709fb4d37e54a8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to