summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/build/changelog/unreleased_12/4016.rst7
-rw-r--r--lib/sqlalchemy/dialects/postgresql/pg8000.py28
-rw-r--r--test/dialect/postgresql/test_types.py6
3 files changed, 35 insertions, 6 deletions
diff --git a/doc/build/changelog/unreleased_12/4016.rst b/doc/build/changelog/unreleased_12/4016.rst
new file mode 100644
index 000000000..bc91614b0
--- /dev/null
+++ b/doc/build/changelog/unreleased_12/4016.rst
@@ -0,0 +1,7 @@
+.. change::
+ :tags: bug, postgresql
+ :tickets: 4016
+
+ Enabled UUID support for the pg8000 driver, which supports native Python
+ uuid round trips for this datatype. Arrays of UUID are still not supported,
+ however. \ No newline at end of file
diff --git a/lib/sqlalchemy/dialects/postgresql/pg8000.py b/lib/sqlalchemy/dialects/postgresql/pg8000.py
index 5f6cd0a3f..3ec7798c3 100644
--- a/lib/sqlalchemy/dialects/postgresql/pg8000.py
+++ b/lib/sqlalchemy/dialects/postgresql/pg8000.py
@@ -70,11 +70,16 @@ from ... import processors
from ... import types as sqltypes
from .base import (
PGDialect, PGCompiler, PGIdentifierPreparer, PGExecutionContext,
- _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES)
+ _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES, UUID)
import re
from sqlalchemy.dialects.postgresql.json import JSON
from ...sql.elements import quoted_name
+try:
+ from uuid import UUID as _python_UUID
+except ImportError:
+ _python_UUID = None
+
class _PGNumeric(sqltypes.Numeric):
def result_processor(self, dialect, coltype):
@@ -113,6 +118,24 @@ class _PGJSON(JSON):
return super(_PGJSON, self).result_processor(dialect, coltype)
+class _PGUUID(UUID):
+ def bind_processor(self, dialect):
+ if not self.as_uuid:
+ def process(value):
+ if value is not None:
+ value = _python_UUID(value)
+ return value
+ return process
+
+ def result_processor(self, dialect, coltype):
+ if not self.as_uuid:
+ def process(value):
+ if value is not None:
+ value = str(value)
+ return value
+ return process
+
+
class PGExecutionContext_pg8000(PGExecutionContext):
pass
@@ -156,7 +179,8 @@ class PGDialect_pg8000(PGDialect):
sqltypes.Numeric: _PGNumericNoBind,
sqltypes.Float: _PGNumeric,
JSON: _PGJSON,
- sqltypes.JSON: _PGJSON
+ sqltypes.JSON: _PGJSON,
+ UUID: _PGUUID
}
)
diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py
index b157070c5..efcf1f0e5 100644
--- a/test/dialect/postgresql/test_types.py
+++ b/test/dialect/postgresql/test_types.py
@@ -1701,7 +1701,6 @@ class UUIDTest(fixtures.TestBase):
'postgresql+zxjdbc',
'column "data" is of type uuid but expression '
'is of type character varying')
- @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
def test_uuid_string(self):
import uuid
self._test_round_trip(
@@ -1716,7 +1715,6 @@ class UUIDTest(fixtures.TestBase):
'postgresql+zxjdbc',
'column "data" is of type uuid but expression is '
'of type character varying')
- @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
def test_uuid_uuid(self):
import uuid
self._test_round_trip(
@@ -1730,7 +1728,7 @@ class UUIDTest(fixtures.TestBase):
@testing.fails_on('postgresql+zxjdbc',
'column "data" is of type uuid[] but '
'expression is of type character varying')
- @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
+ @testing.fails_on('postgresql+pg8000', 'No support for UUID with ARRAY')
def test_uuid_array(self):
import uuid
self._test_round_trip(
@@ -1745,7 +1743,7 @@ class UUIDTest(fixtures.TestBase):
@testing.fails_on('postgresql+zxjdbc',
'column "data" is of type uuid[] but '
'expression is of type character varying')
- @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
+ @testing.fails_on('postgresql+pg8000', 'No support for UUID with ARRAY')
def test_uuid_string_array(self):
import uuid
self._test_round_trip(