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

No default encoding (2.0.0a1) #162

Open
ghost opened this issue Apr 9, 2018 · 3 comments
Open

No default encoding (2.0.0a1) #162

ghost opened this issue Apr 9, 2018 · 3 comments
Labels

Comments

@ghost
Copy link

ghost commented Apr 9, 2018

I kept getting the error AttributeError: 'DatabaseWrapper' object has no attribute 'encoding' and it turns out that encoding is only set by default in this block of base.py if there is an OPTIONS key who's value is a dictionary with least one key.

Workaround: add an OPTIONS key to your config that has a populated dictionary

Possible solutions:

  1. Check and set base.py after line 202 with something like
        self.encoding = 'utf-8'

and also update the README.rst to reflect that encoding isn't only set to 'utf-8' by default when OPTIONS is specified

  1. Change line 394 in base.py from 'return CursorWrapper(cursor, self.driver_supports_utf8, self.encoding, self)' to 'return CursorWrapper(cursor, self.driver_supports_utf8, getattr(self, 'encoding'), self)

CursorWrapper's init function is ready to handle None/empty string values, so I'm not sure which solution works better behind the goals of the project.

@dlo dlo added the bug label Apr 9, 2018
@RossRogers
Copy link
Contributor

RossRogers commented Apr 9, 2018

@rcucui,
I don't see what line you're talking about for this error:

AttributeError: 'DatabaseWrapper' object has no attribute 'encoding' 

Your link labeled "in this block of base.py" points to this line:

self.MARS_Connection = options.get('MARS_Connection', False)

@ghost
Copy link
Author

ghost commented Apr 10, 2018

@RossRogers I went one line too low into the if options: block instead of the start of the block.
Updated the link.

The original traceback raises an exception at line 394:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10fb4a6a8>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 123, in inner_run
    self.check_migrations()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 427, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/loader.py", line 206, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
    if self.has_table():
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 44, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django_pyodbc/base.py", line 356, in _cursor
    if self.ops.sql_server_ver < 2005:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django_pyodbc/operations.py", line 130, in _get_sql_server_ver
    cur = self.connection.cursor()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django_pyodbc/base.py", line 394, in _cursor
    return CursorWrapper(cursor, self.driver_supports_utf8, self.encoding, self)
AttributeError: 'DatabaseWrapper' object has no attribute 'encoding'

@RossRogers
Copy link
Contributor

I guess we can change like:

diff --git a/django_pyodbc/base.py b/django_pyodbc/base.py
index dab5399..07357de 100644
--- a/django_pyodbc/base.py
+++ b/django_pyodbc/base.py
@@ -392,7 +392,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
                 self.driver_supports_utf8 = (self.drv_name == 'SQLSRV32.DLL'
                                              or ms_sqlncli.match(self.drv_name))

-        return CursorWrapper(cursor, self.driver_supports_utf8, self.encoding, self)
+        return CursorWrapper(cursor, self.driver_supports_utf8, getattr(self, 'encoding', 'utf-8'), self)

     def _execute_foreach(self, sql, table_names=None):
         cursor = self.cursor()

Anyways, I'm not sure how functional the library is without the database configuration.

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

No branches or pull requests

2 participants