summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql_psycopg2/operations.py
diff options
context:
space:
mode:
authorMichaƂ Modzelewski <michal.modzelewski@gmail.com>2015-01-02 02:39:31 +0100
committerTim Graham <timograham@gmail.com>2015-01-12 18:15:34 -0500
commit65246de7b1d70d25831ab394c4f4a75813f629fe (patch)
tree618c5f030f9a77d240dc59b132dd1e152baca116 /django/db/backends/postgresql_psycopg2/operations.py
parentaa8ee6a5731b37b73635e7605521fb1a54a5c10d (diff)
downloaddjango-65246de7b1d70d25831ab394c4f4a75813f629fe.tar.gz
Fixed #24031 -- Added CASE expressions to the ORM.
Diffstat (limited to 'django/db/backends/postgresql_psycopg2/operations.py')
-rw-r--r--django/db/backends/postgresql_psycopg2/operations.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py
index 9d5b10d01a..31cbe5919f 100644
--- a/django/db/backends/postgresql_psycopg2/operations.py
+++ b/django/db/backends/postgresql_psycopg2/operations.py
@@ -5,6 +5,18 @@ from django.db.backends import BaseDatabaseOperations
class DatabaseOperations(BaseDatabaseOperations):
+ def unification_cast_sql(self, output_field):
+ internal_type = output_field.get_internal_type()
+ if internal_type in ("GenericIPAddressField", "IPAddressField", "TimeField", "UUIDField"):
+ # PostgreSQL will resolve a union as type 'text' if input types are
+ # 'unknown'.
+ # http://www.postgresql.org/docs/9.4/static/typeconv-union-case.html
+ # These fields cannot be implicitly cast back in the default
+ # PostgreSQL configuration so we need to explicitly cast them.
+ # We must also remove components of the type within brackets:
+ # varchar(255) -> varchar.
+ return 'CAST(%%s AS %s)' % output_field.db_type(self.connection).split('(')[0]
+ return '%s'
def date_extract_sql(self, lookup_type, field_name):
# http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT