From 32d5c39016f7f14dfc19142d78d944d92eb9cd70 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 8 Aug 2008 20:09:53 +0000 Subject: [PATCH] Fixed #6523 -- Use the correct cast on field types for PostgreSQL when searching within a field column (e.g. "like", "contains", etc). Required for PostgreSQL 8.3. Thanks to Dan Watson for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8242 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/postgresql/operations.py | 6 ++++++ tests/modeltests/lookup/models.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index ba6e3235c2..de7b5a9520 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -35,6 +35,12 @@ class DatabaseOperations(BaseDatabaseOperations): def deferrable_sql(self): return " DEFERRABLE INITIALLY DEFERRED" + def lookup_cast(self, lookup_type): + if lookup_type in ('iexact', 'contains', 'icontains', 'startswith', 'istartswith', + 'endswith', 'iendswith'): + return "%s::text" + return "%s" + def field_cast_sql(self, db_type): if db_type == 'inet': return 'HOST(%s)' diff --git a/tests/modeltests/lookup/models.py b/tests/modeltests/lookup/models.py index 3261a678d6..1d39e0e22b 100644 --- a/tests/modeltests/lookup/models.py +++ b/tests/modeltests/lookup/models.py @@ -34,6 +34,12 @@ __test__ = {'API_TESTS':r""" >>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 27)) >>> a7.save() +# text matching tests for PostgreSQL 8.3 +>>> Article.objects.filter(id__iexact='1') +[] +>>> Article.objects.filter(pub_date__startswith='2005') +[, , , , , , ] + # Each QuerySet gets iterator(), which is a generator that "lazily" returns # results using database-level iteration. >>> for a in Article.objects.iterator():