Krzysztof Krysztofczyk
Krzysztof Krysztofczyk

Reputation: 499

Django with SQL Server – GUID Type is mapped as char(32) not uniqueidentifier

I would like an app that can easily use different databases. Currently, I’m using SQLite and SQL Server, switching between them via a config file. I noticed that the following field could be quite important from a performance standpoint:

user_guid = models.UUIDField(
    default=uuid.uuid4,  
    editable=False,      
    unique=True,          
    null=False,          
    blank=False,         
    db_index=True        
)

In SQL Server, the field is mapped as char(32), and I have mixed feelings about that. Over time, I might switch to PostgreSQL, but as I mentioned before, I’m not entirely comfortable using char(32) instead of uniqueidentifier.

Upvotes: 1

Views: 138

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477533

This is because it is specified like a char(32) [GitHub]:

class DatabaseWrapper(BaseDatabaseWrapper):
     # …
     data_types = {
         # …
         <b>'UUIDField': 'char(32)'</b>,
     }
     # &hellip;

Changing it is also not easy: SQL Server has no native UUID column type, one can use a BINARY(16), but there is nothing that accepts a UUID itself. The UUIDField converts a value to a value for the database with [GitHub]:

class UUIDField(Field):
     # &hellip;
 
     def get_db_prep_value(self, value, connection, prepared=False):
         if value is None:
             return None
         if not isinstance(value, uuid.UUID):
             value = self.to_python(value)
 
         if <b>connection.features.has_native_uuid_field</b>:
             return value
         return value.hex
 
     # &hellip;

So either it works with the UUID object itself, or with the .hex of a UUID field, so what a VARCHAR essentially stores.

Databases that have a native UUID field, like PostgreSQL, use that type.

Upvotes: 2

iklinac
iklinac

Reputation: 15748

pyodbc had no support for conversion from uniqueidentifer/SQL_GUID to UUID at the time of package creation (pyodbc native support issue)

As following change was implemented, writing uniqueidentifer instead of char(32) implementation was straight forward

That being said lack of interest in using SQL Server with Django and therefore lack of official support is probably reason why this is not implemented as part of package

Update:

Upvotes: 1

Related Questions