From 6060ee7c77819623c145e1911d000325ed3655c5 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 17 Jul 2009 00:15:02 +0000 Subject: [PATCH] [soc2009/multidb] Fixed a bug with EmptyQuerySet. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11257 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/commands/createcachetable.py | 3 ++- django/db/backends/creation.py | 3 ++- django/db/models/fields/related.py | 3 ++- django/db/models/query.py | 7 ++++++- django/db/models/sql/where.py | 3 ++- django/db/utils.py | 8 ++++++++ 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/django/core/management/commands/createcachetable.py b/django/core/management/commands/createcachetable.py index 5da5fb7118..30eaa06c21 100644 --- a/django/core/management/commands/createcachetable.py +++ b/django/core/management/commands/createcachetable.py @@ -2,6 +2,7 @@ from optparse import make_option from django.core.management.base import LabelCommand from django.db import connections, transaction, models, DEFAULT_DB_ALIAS +from django.db.utils import call_with_connection class Command(LabelCommand): help = "Creates the table needed to use the SQL cache backend." @@ -30,7 +31,7 @@ class Command(LabelCommand): index_output = [] qn = connection.ops.quote_name for f in fields: - field_output = [qn(f.name), f.db_type(connection)] + field_output = [qn(f.name), call_with_connection(f.db_type, connection=connection)] field_output.append("%sNULL" % (not f.null and "NOT " or "")) if f.primary_key: field_output.append("PRIMARY KEY") diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py index 47bf13a226..e3bb88cc8f 100644 --- a/django/db/backends/creation.py +++ b/django/db/backends/creation.py @@ -8,6 +8,7 @@ except NameError: from django.conf import settings from django.core.management import call_command +from django.db.utils import call_with_connection # The prefix to put on the default database name when creating # the test database. @@ -47,7 +48,7 @@ class BaseDatabaseCreation(object): pending_references = {} qn = self.connection.ops.quote_name for f in opts.local_fields: - col_type = f.db_type(self.connection) + col_type = call_with_connection(f.db_type, connection=self.connection) tablespace = f.db_tablespace or opts.db_tablespace if col_type is None: # Skip ManyToManyFields, because they're not represented as diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index eef2fb2754..47f5c68fed 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -5,6 +5,7 @@ from django.db.models.fields import AutoField, Field, IntegerField, PositiveInte from django.db.models.related import RelatedObject from django.db.models.query import QuerySet from django.db.models.query_utils import QueryWrapper +from django.db.utils import call_with_connection from django.utils.encoding import smart_unicode from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _ from django.utils.functional import curry @@ -772,7 +773,7 @@ class ForeignKey(RelatedField, Field): isinstance(rel_field, (PositiveIntegerField, PositiveSmallIntegerField)))): return IntegerField().db_type(connection) - return rel_field.db_type(connection) + return call_with_connection(rel_field.db_type, connection=connection) class OneToOneField(ForeignKey): """ diff --git a/django/db/models/query.py b/django/db/models/query.py index 1e91badccd..edfded0d03 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -33,7 +33,12 @@ class QuerySet(object): """ def __init__(self, model=None, query=None): self.model = model - using = model._meta.using or DEFAULT_DB_ALIAS + # EmptyQuerySet instantiates QuerySet with model as None + if model: + using = model._meta.using + else: + using = None + using = using or DEFAULT_DB_ALIAS connection = connections[using] self.query = query or sql.Query(self.model, connection) self._result_cache = None diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index b8f921a20e..ec0bfdfe9e 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -6,6 +6,7 @@ import datetime from django.utils import tree from django.db.models.fields import Field from django.db.models.query_utils import QueryWrapper +from django.db.utils import call_with_connection from datastructures import EmptyResultSet, FullResultSet # Connection types @@ -266,7 +267,7 @@ class Constraint(object): try: if self.field: params = self.field.get_db_prep_lookup(lookup_type, value) - db_type = self.field.db_type(connection) + db_type = call_with_connection(self.field.db_type, connection=connection) else: # This branch is used at times when we add a comparison to NULL # (we don't really want to waste time looking up the associated diff --git a/django/db/utils.py b/django/db/utils.py index 553bbca48d..6b78e5b91e 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -1,3 +1,4 @@ +import inspect import os from django.conf import settings @@ -31,6 +32,13 @@ def load_backend(backend_name): else: raise # If there's some other error, this must be an error in Django itself. +def call_with_connection(func, *args, **kwargs): + arg_names, varargs, varkwargs, defaults = inspect.getargspec(func) + if 'connection' not in arg_names and varkwargs is None: + del kwargs['connection'] + return func(*args, **kwargs) + + class ConnectionHandler(object): def __init__(self, databases): self.databases = databases