mysql reflection on python-2.6 causes error

Issue #3123 resolved
barnabas79 created an issue

The essential problem is that mysql seems to return some table names as unicode, and in python 2.6, you can't use unicode as keys in a dict that you're unpacking for function kwargs - ie, in python 2.6, this:

def foo(**kwargs):
    print kwargs
foo(**{u'thing':1})

will raise an error:

TypeError: foo() keywords must be strings

To replicate:

  • Set up a mysql server
    • On fedora 19, I just did:
      • yum install mysql-server
      • service mysqld start
  • Using python-2.6, run the following code (assuming you're connecting to the server running on the same machine, and that there's a database called "test", which should exist by default):
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey

# connect to the "test" database, which should be set up by default

engine = create_engine("mysql+mysqldb://127.0.0.1/test", echo=True)

# Set up a basic table

metadata = MetaData()
users = Table(
    'users', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(255)),
)
metadata.create_all(engine)

# now reflect..

metadata2 = MetaData()
metadata2.reflect(engine, only=['users'])

This should raise this error:

Traceback (most recent call last):
  File "<stdin>", line 22, in <module>
    metadata2.reflect(engine, only=['users'])
  File "./sqlalchemy/lib/sqlalchemy/sql/schema.py", line 3288, in reflect
    Table(name, self, **reflect_opts)
  File "./sqlalchemy/lib/sqlalchemy/sql/schema.py", line 353, in __new__
    table._init(name, metadata, *args, **kw)
  File "./sqlalchemy/lib/sqlalchemy/sql/schema.py", line 426, in _init
    self._autoload(metadata, autoload_with, include_columns)
  File "./sqlalchemy/lib/sqlalchemy/sql/schema.py", line 438, in _autoload
    self, include_columns, exclude_columns
  File "./sqlalchemy/lib/sqlalchemy/engine/base.py", line 1239, in run_callable
    return callable_(self, *args, **kwargs)
  File "./sqlalchemy/lib/sqlalchemy/engine/default.py", line 356, in reflecttable
    return insp.reflecttable(table, include_columns, exclude_columns)
  File "./sqlalchemy/lib/sqlalchemy/engine/reflection.py", line 474, in reflecttable
    for col_d in self.get_columns(table_name, schema, **table.dialect_kwargs):
TypeError: get_columns() keywords must be strings

Comments (8)

  1. Mike Bayer repo owner

    ok surprise #1 on my end...need to figure out this:

    classic$ python2.6
    Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51) 
    [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def foo(**kwargs):
    ...     print kwargs
    ... 
    >>> foo(**{u'thing':1})
    {u'thing': 1}
    >>> 
    
  2. Mike Bayer repo owner

    was fixed in python 2.6.5. going to see if i can get an older py2.6 running, but if this issue has any complexity to it it's looking like it can be skipped....

  3. Mike Bayer repo owner
    • Added a "str()" step to the dialect_kwargs iteration for Python version < 2.6.5, working around the "no unicode keyword arg" bug as these args are passed along as keyword args within some reflection processes. fixes #3123

    → <<cset a49ec1cf7788>>

  4. Mike Bayer repo owner
    • Added a "str()" step to the dialect_kwargs iteration for Python version < 2.6.5, working around the "no unicode keyword arg" bug as these args are passed along as keyword args within some reflection processes. fixes #3123

    → <<cset aba5b6e5c28c>>

  5. Log in to comment