Quering (with filter) a table with spaces in their fieldname, crashes

Issue #1260 resolved
Former user created an issue

Hi,

I'm the guy dealing with Microsoft Dynamics NAV (the ERP with strange symbols in its fieldnames).

Now I found out that when I query a table with some filters sqlalchemy raises the following exception:

Traceback (most recent call last):
  File "exportOrdersFromNAV2.py", line 263, in <module>
    ).filter(PurchaseLine.no != u''
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\o
rm\query.py", line 998, in all
    return list(self)
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\o
rm\query.py", line 1088, in __iter__
    return self._execute_and_instances(context)
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\o
rm\query.py", line 1091, in _execute_and_instances
    result = self.session.execute(querycontext.statement, params=self._params, m
apper=self._mapper_zero_or_none(), _state=self._refresh_state)
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\o
rm\session.py", line 750, in execute
    clause, params or {})
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\e
ngine\base.py", line 806, in execute
    return Connection.executors[c](c)(self, object, multiparams, params)
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\e
ngine\base.py", line 853, in execute_clauseelement
    compiled=elem.compile(dialect=self.dialect, column_keys=keys, inline=len(par
ams) > 1),
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\expression.py", line 1194, in compile
    compiler.compile()
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 196, in compile
    self.string = self.process(self.statement)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 199, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "<string>", line 2, in _compiler_dispatch
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\d
atabases\mssql.py", line 976, in visit_select
    return compiler.DefaultCompiler.visit_select(self, select, **kwargs)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 521, in visit_select
    t = self.process(select._whereclause)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 199, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "<string>", line 2, in _compiler_dispatch
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 324, in visit_clauselist
    return sep.join(s for s in (self.process(c) for c in clauselist.clauses)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 324, in <genexpr>
    return sep.join(s for s in (self.process(c) for c in clauselist.clauses)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 324, in <genexpr>
    return sep.join(s for s in (self.process(c) for c in clauselist.clauses)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 199, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "<string>", line 2, in _compiler_dispatch
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 324, in visit_clauselist
    return sep.join(s for s in (self.process(c) for c in clauselist.clauses)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 324, in <genexpr>
    return sep.join(s for s in (self.process(c) for c in clauselist.clauses)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 324, in <genexpr>
    return sep.join(s for s in (self.process(c) for c in clauselist.clauses)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 199, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "<string>", line 2, in _compiler_dispatch
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\d
atabases\mssql.py", line 1033, in visit_binary
    return super(MSSQLCompiler, self).visit_binary(binary, **kwargs)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 383, in visit_binary
    return self.process(binary.left) + " " + op + " " + self.process(binary.righ
t)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 199, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "<string>", line 2, in _compiler_dispatch
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 389, in visit_bindparam
    name = self._truncate_bindparam(bindparam)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 403, in _truncate_bindparam
    self._truncated_identifier("bindparam", bind_name) or bind_name
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 415, in _truncated_identifier
    anonname = name % self.anon_map
  File "c:\python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\u
til.py", line 57, in __missing__
    self[key](key) = val = self.creator(key)
  File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
ql\compiler.py", line 430, in _process_anon
    (ident, derived) = key.split(' ')
ValueError: too many values to unpack

I tried the following patch with success:

--- lib/sqlalchemy/compiler.py.orig    2008-12-23 12:54:16.000000000 +0100
+++ lib/sqlalchemy/compiler.py 2008-12-23 12:54:35.000000000 +0100
@@ -425,7 +425,7 @@
         return name % self.anon_map

     def _process_anon(self, key):
-        (ident, derived) = key.split(' ')
+        (ident, derived) = key.split(' ', 1)

         anonymous_counter = self.anon_map.get(derived, 1)
         self.anon_map[derived](derived) = anonymous_counter + 1

Comments (2)

  1. Mike Bayer repo owner

    You're not running against the current trunk. This line from your stack trace illustrates it:

    File "C:\Python26\lib\site-packages\sqlalchemy-0.5.0rc4-py2.6.egg\sqlalchemy\s
    ql\compiler.py", line 415, in _truncated_identifier
        anonname = name % self.anon_map
    

    the version number in the file path does not include the SVN revision, and also line 415 after the fix now reads:

            anonname = self._trunc_re.sub(lambda m: self.anon_map[m.group(1)](m.group(1)), name)
    
  2. Log in to comment