KeyError when reflecting mssql tables that contains indexes with non alphanumeric chars in field names

Issue #1770 resolved
Former user created an issue

I tried to reflect some tables I have in mssql but obtain the following error:

Traceback (most recent call last):
  File "/usr/local/Plone/public_test/bin/zopepy", line 146, in ?
    execfile(sys.argv[0](0))
  File "./test.py", line 11, in ?
    customer = Table('Bizak 26-03-2010$Customer', meta, autoload=True, autoload_with=engine)
  File "/usr/local/Plone/buildout-cache/eggs/SQLAlchemy-0.6beta3-py2.4.egg/sqlalchemy/schema.py", line 207, in __new__
    table._init(name, metadata, *args, **kw)
  File "/usr/local/Plone/buildout-cache/eggs/SQLAlchemy-0.6beta3-py2.4.egg/sqlalchemy/schema.py", line 253, in _init
    autoload_with.reflecttable(self, include_columns=include_columns)
  File "/usr/local/Plone/buildout-cache/eggs/SQLAlchemy-0.6beta3-py2.4.egg/sqlalchemy/engine/base.py", line 1588, in reflecttable
    self.dialect.reflecttable(conn, table, include_columns)
  File "/usr/local/Plone/buildout-cache/eggs/SQLAlchemy-0.6beta3-py2.4.egg/sqlalchemy/engine/default.py", line 206, in reflecttable
    return insp.reflecttable(table, include_columns)
  File "/usr/local/Plone/buildout-cache/eggs/SQLAlchemy-0.6beta3-py2.4.egg/sqlalchemy/engine/reflection.py", line 369, in reflecttable
    sa_schema.Index(name, *[table.columns[c](table.columns[c) for c in columns], 
  File "/usr/local/Plone/buildout-cache/eggs/SQLAlchemy-0.6beta3-py2.4.egg/sqlalchemy/util.py", line 730, in __getitem__
    return self._data[key](key)
KeyError: 'Search'

In my tests one of the values of rowindex_keys (dialect.mssql.base line 1105) was 'Search Name, No_' then applying col_finder.findall('Search Name, No_') results in Name', No_'. Three fields!!! Actually there are two fields called 'Search Name' and 'No_'.

Attached you can find a patch to solve this.

Comments (6)

  1. Mike Bayer repo owner

    Unfortunately the attached patch does not accommodate column names that have commas in them, which are reported by sp_helpindex with no unambiguous delineation. While I would wonder why a column name would ever have a comma in it, I wonder exactly the same thing about column names with spaces in them, and SQL Server accepts both, so therefore we have to worry about both.

    9c0d6c0a2326d00579c87c140890e6a9b65b6d32 contains a rewrite of get_indexes() which uses the sys. tables. This implies that index reflection is only supported for SQL Server 2005 and above, which I'm fine with (since index reflection is already not a typical need).

  2. Mike Bayer repo owner
    • changed status to open
    • removed status

    the query is not correct and test.engine.test_reflection fails for mssql.

  3. Log in to comment