mssql fails to reflect indexes referencing non-ascii column names

Issue #2269 resolved
Former user created an issue

I'm encountering a SQL Server database with non-ascii column names (containing Icelandic characters). Autoload fails on indexes referencing such columns:

from sqlalchemy import *
some_url = 'mssql+pyodbc://someuser:somepass@/?Servername=Somename'
e = create_engine(some_url, encoding='utf8', convert_unicode=True)
metadata = MetaData(e)
tables = [metadata, autoload=True) for tn in e.table_names()](Table(tn,)

results in:

Traceback (most recent call last):
  File "mssql_non_ascii_column_names.py", line 8, in <module>
    tables = [metadata, autoload=True) for tn in e.table_names()](Table(tn,)
  File "/opt/pyenv/FOO/lib/python2.5/site-packages/sqlalchemy/schema.py", line 265, in __new__
    table._init(name, metadata, *args, **kw)
  File "/opt/pyenv/FOO/lib/python2.5/site-packages/sqlalchemy/schema.py", line 335, in _init
    self, include_columns
  File "/opt/pyenv/FOO/lib/python2.5/site-packages/sqlalchemy/engine/base.py", line 2264, in run_callable
    return conn.run_callable(callable_, *args, **kwargs)
  File "/opt/pyenv/FOO/lib/python2.5/site-packages/sqlalchemy/engine/base.py", line 1894, in run_callable
    return callable_(self, *args, **kwargs)
  File "/opt/pyenv/FOO/lib/python2.5/site-packages/sqlalchemy/engine/default.py", line 254, in reflecttable
    return insp.reflecttable(table, include_columns)
  File "/opt/pyenv/FOO/lib/python2.5/site-packages/sqlalchemy/engine/reflection.py", line 470, in reflecttable
    sa_schema.Index(name, *[table.columns[c](table.columns[c) for c in columns], 
  File "/opt/pyenv/FOO/lib/python2.5/site-packages/sqlalchemy/util/_collections.py", line 88, in __getitem__
    return self._data[key](key)
KeyError: 'S\xc3\x96LUGENGI'

Attached patch fixes this by decoding the column names according to the dialect's self._decoder if they aren't already unicode.

I don't have a SQL Server with write access to run regression tests against, but at least this doesn't break any of the 46 tests that ./sqla_nose.py test.dialect.test_mssql can run without a DB backend.

Comments (8)

  1. Former user Account Deleted

    Yep, not quite ready to move to 0.7 yet (and this fits equally neatly on both branches, hence the two patches).

  2. Mike Bayer repo owner

    backport to 0.6.9 as well (I'm reversing how i assign milestones, otherwise the issue looks like an 0.6-only thing)

  3. Log in to comment