From dce38541411a41fb672499950f51bab7bbd9fef9 Mon Sep 17 00:00:00 2001 From: dauinsight <145612907+dauinsight@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:05:52 -0800 Subject: [PATCH] Support UUID (#329) * Add support for UUID --- mssql/base.py | 6 ++++-- mssql/features.py | 3 ++- mssql/operations.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/mssql/base.py b/mssql/base.py index f0be8474..033660c2 100644 --- a/mssql/base.py +++ b/mssql/base.py @@ -124,7 +124,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): 'SmallIntegerField': 'smallint', 'TextField': 'nvarchar(max)', 'TimeField': 'time', - 'UUIDField': 'char(32)', + 'UUIDField': 'uniqueidentifier', } data_types_suffix = { 'AutoField': 'IDENTITY (1, 1)', @@ -376,7 +376,6 @@ def get_new_connection(self, conn_params): break if not need_to_retry: raise - # Handling values from DATETIMEOFFSET columns # source: https://github.com/mkleehammer/pyodbc/wiki/Using-an-Output-Converter-function conn.add_output_converter(SQL_TIMESTAMP_WITH_TIMEZONE, handle_datetimeoffset) @@ -431,6 +430,9 @@ def init_connection_state(self): if (options.get('return_rows_bulk_insert', False)): self.features_class.can_return_rows_from_bulk_insert = True + if (options.get('has_native_uuid_field', True)): + Database.native_uuid = True + val = self.get_system_datetime if isinstance(val, str): raise ImproperlyConfigured( diff --git a/mssql/features.py b/mssql/features.py index c2158072..b2480f52 100644 --- a/mssql/features.py +++ b/mssql/features.py @@ -22,7 +22,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): has_json_object_function = False has_json_operators = False has_native_json_field = False - has_native_uuid_field = False + has_native_uuid_field = True has_real_datatype = True has_select_for_update = True has_select_for_update_nowait = True @@ -64,6 +64,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): supports_stored_generated_columns = True supports_virtual_generated_columns = True + @cached_property def has_zoneinfo_database(self): with self.connection.cursor() as cursor: diff --git a/mssql/operations.py b/mssql/operations.py index b83305a2..6a00c835 100644 --- a/mssql/operations.py +++ b/mssql/operations.py @@ -129,7 +129,7 @@ def convert_floatfield_value(self, value, expression, connection): def convert_uuidfield_value(self, value, expression, connection): if value is not None: - value = uuid.UUID(value) + value = uuid.UUID(str(value)) return value def convert_booleanfield_value(self, value, expression, connection):