lionheart / django-pyodbc

An ODBC-powered MS SQL Server DB backend for Django 1.4+
Apache License 2.0
203 stars 103 forks source link

DatabaseWrapper has no data_types #128

Closed chris-nlnz closed 7 years ago

chris-nlnz commented 7 years ago

Have an issue using django-pyodbc 1.0.0 with Django 1.8.15.

When saving (inserting) an object to a pyodbc database I get a TypeError when pyodbc tries to get the pk_db_type (compiler.py line 533):

# mangle SQL to return ID from insert
# http://msdn.microsoft.com/en-us/library/ms177564.aspx
if self.return_id and self.connection.features.can_return_id_from_insert:
    col = self.connection.ops.quote_name(meta.pk.db_column or meta.pk.get_attname())
    # Determine datatype for use with the table variable that will return the inserted ID
    pk_db_type = _re_data_type_terminator.split(meta.pk.db_type(self.connection))[0]

Problem is that the django.db.models.fields.Field.db_type method in Django 1.8.15 tries to get the data_type from a different place than Django 1.7 did:

# Django 1.7
data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
try:
    return connection.creation.data_types[self.get_internal_type()] % data
except KeyError:
    return None

# Django 1.8
data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
try:
    return connection.data_types[self.get_internal_type()] % data
except KeyError:
    return None

Looking at pyodbc's code I see there is a data_types attribute on the DatabaseCreation model, but not on the DatabaseWrapper, whereas it seems to be the other way around with Django's built in DB engines. So this call fails as connection.data_types doesn't have any types in it, returns None and the regexp split fails as it's not a string.

Hope that makes sense, I don't know too much about django-pyodbc/Django database engine internals apart from what I discovered today so I apologise if I have misunderstood anything here.

chris-nlnz commented 7 years ago

The change is mentioned here in Django's release notes: https://docs.djangoproject.com/en/1.10/releases/1.8/#database-backend-api

"The data_types, data_types_suffix, and data_type_check_constraints attributes have moved from the DatabaseCreation class to DatabaseWrapper."

RossRogers commented 7 years ago

Fix my M. Chris in merge request #129.