使用符合PEP 435的枚举的枚举的SQLAlchemy列会产生表创建错误

时间:2016-06-24 19:21:07

标签: python orm enums sqlalchemy

我有一个枚举...与here非常相似。

当我尝试使用枚举创建表时,如示例中所示,在表创建期间出现错误。这是一个非常模糊的错误。这是复制问题的代码:

from sqlalchemy import Table, MetaData, Column, Enum, create_engine
import enum
class myEnum(enum.Enum):
    one = 'one'
    two = 'two'
    three = 'three'

def main():
    e = create_engine('sqlite:///:memory:')
    e.echo = True
    m = MetaData(bind = e)
    t = Table('table', m, Column('my_enum', Enum(myEnum)))
    t.create()
if __name__ == '__main__': main()

当我运行此代码时,我得到一个AttributeError: replace,看似无用的堆栈跟踪。老实说,我甚至不知道从哪里开始调试这个,虽然我可以设想一些可能的解决方法,但我对SQLAlchemy相对较新,所以我不确定最干净的解决方案是什么。当然,基本的枚举支持是对ORM框架的一个相对简单的期望吗?

这是完整的堆栈跟踪:

Traceback (most recent call last):
  File "table_test.py", line 17, in <module>
    if __name__ == '__main__': main()
  File "table_test.py", line 16, in main
    t.create()
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\schema.py", line 725, in create
    checkfirst=checkfirst)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1856, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 1481, in _run_visitor
    **kwargs).traverse_single(element)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 121, in traverse_single
    return meth(obj, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\ddl.py", line 764, in visit_table
    include_foreign_key_constraints=include_foreign_key_constraints
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 914, in execute
    return meth(self, multiparams, params)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\ddl.py", line 68, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\engine\base.py", line 962, in _execute_ddl
    compiled = ddl.compile(dialect=dialect)
  File "<string>", line 1, in <lambda>
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\elements.py", line 494, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\ddl.py", line 26, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 190, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2173, in visit_create_table
    create.include_foreign_key_constraints)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2220, in create_table_constraints
    for constraint in constraints
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2218, in <genexpr>
    p for p in
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2226, in <genexpr>
    not getattr(constraint, 'use_alter', False)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 93, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 2369, in visit_check_constraint
    literal_binds=True)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 213, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 927, in visit_binary
    return self._generate_generic_binary(binary, opstring, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 944, in _generate_generic_binary
    binary.right._compiler_dispatch(self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 527, in visit_grouping
    return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 751, in visit_clauselist
    for c in clauselist.clauses)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 748, in <genexpr>
    s for s in
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 751, in <genexpr>
    for c in clauselist.clauses)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\visitors.py", line 81, in _compiler_dispatch
    return meth(self, **kw)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 1071, in visit_bindparam
    bindparam, within_columns_clause=True, **kwargs)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 1103, in render_literal_bindparam
    return self.render_literal_value(value, bindparam.type)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\compiler.py", line 1118, in render_literal_value
    return processor(value)
  File "C:\Anaconda3\lib\site-packages\sqlalchemy\sql\sqltypes.py", line 171, in process
    value = value.replace("'", "''")
  File "C:\Anaconda3\lib\enum.py", line 268, in __getattr__
    raise AttributeError(name) from None
AttributeError: replace

1 个答案:

答案 0 :(得分:6)

1.1中已添加

PEP-435 enum support

1.1.0b1最近发布。您可以升级到1.1.0b1但要小心bug。根据发布历史,我会说稳定版本应该在一两个月内发布。

如何升级:

pip install 'sqlalchemy==1.1.0b3'