Commits

Al...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed b0ae67b

[soc2009/multidb] Correct the handling of raw and defered fields with multi-db.

  • Participants
  • Parent commits ad02139
  • Branches soc2009/multidb

Comments (0)

Files changed (5)

File django/db/models/manager.py

         return self.get_query_set()._update(values, **kwargs)
 
     def raw(self, query, params=None, *args, **kwargs):
+        kwargs["using"] = self.db
         return RawQuerySet(model=self.model, query=query, params=params, *args, **kwargs)
 
 class ManagerDescriptor(object):

File django/db/models/query.py

     Provides an iterator which converts the results of raw SQL queries into
     annotated model instances.
     """
-    def __init__(self, query, model=None, query_obj=None, params=None, translations=None):
+    def __init__(self, query, model=None, query_obj=None, params=None,
+        translations=None, using=None):
         self.model = model
-        self.query = query_obj or sql.RawQuery(sql=query, connection=connection, params=params)
+        self.using = using
+        self.query = query_obj or sql.RawQuery(sql=query, connection=connections[using], params=params)
         self.params = params or ()
         self.translations = translations or {}
 
 
         for field, value in annotations:
             setattr(instance, field, value)
+        
+        instance._state.db = self.using
 
         return instance
 

File django/db/models/query_utils.py

         cls = self.model_ref()
         data = instance.__dict__
         if data.get(self.field_name, self) is self:
-            data[self.field_name] = cls._base_manager.filter(pk=instance.pk).values_list(self.field_name, flat=True).get()
+            data[self.field_name] = cls._base_manager.filter(pk=instance.pk).values_list(self.field_name, flat=True).using(instance._state.db).get()
         return data[self.field_name]
 
     def __set__(self, instance, value):

File django/db/models/sql/query.py

 from django.utils.tree import Node
 from django.utils.datastructures import SortedDict
 from django.utils.encoding import force_unicode
-from django.db import connection, connections, DEFAULT_DB_ALIAS
+from django.db import connections, DEFAULT_DB_ALIAS
 from django.db.models import signals
 from django.db.models.fields import FieldDoesNotExist
 from django.db.models.query_utils import select_related_descend, InvalidQuery

File tests/regressiontests/multiple_database/tests.py

 
         self.assertEquals(learn.get_next_by_published().title, "Dive into Python")
         self.assertEquals(dive.get_previous_by_published().title, "Learning Python")
+    
+    def test_raw(self):
+        "test the raw() method across databases"
+        dive = Book.objects.using('other').create(title="Dive into Python",
+            published=datetime.date(2009, 5, 4))
+        val = Book.objects.db_manager("other").raw('SELECT id FROM "multiple_database_book"')
+        self.assertEqual(map(lambda o: o.pk, val), [dive.pk])
 
 
 class UserProfileTestCase(TestCase):