Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DatabaseWrapper has no data_types #128

Closed
chris-nlnz opened this issue Nov 15, 2016 · 2 comments
Closed

DatabaseWrapper has no data_types #128

chris-nlnz opened this issue Nov 15, 2016 · 2 comments

Comments

@chris-nlnz
Copy link

chris-nlnz commented Nov 15, 2016

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
Copy link
Author

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."

chris-nlnz pushed a commit to chris-nlnz/django-pyodbc that referenced this issue Nov 17, 2016
RossRogers added a commit that referenced this issue Nov 17, 2016
Fix data_types incompatibility with Django 1.8+, issue #128.
@RossRogers
Copy link
Contributor

Fix my M. Chris in merge request #129.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants