我有一个enum…定义与这里非常相似。
当我尝试使用枚举创建表时,如示例中所示,我在创建表时得到一个错误。这是一个非常模糊的错误。下面是复制这个问题的代码:
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:Anaconda3libsite-packagessqlalchemysqlschema.py", line 725, in create
checkfirst=checkfirst)
File "C:Anaconda3libsite-packagessqlalchemyenginebase.py", line 1856, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "C:Anaconda3libsite-packagessqlalchemyenginebase.py", line 1481, in _run_visitor
**kwargs).traverse_single(element)
File "C:Anaconda3libsite-packagessqlalchemysqlvisitors.py", line 121, in traverse_single
return meth(obj, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlddl.py", line 764, in visit_table
include_foreign_key_constraints=include_foreign_key_constraints
File "C:Anaconda3libsite-packagessqlalchemyenginebase.py", line 914, in execute
return meth(self, multiparams, params)
File "C:Anaconda3libsite-packagessqlalchemysqlddl.py", line 68, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "C:Anaconda3libsite-packagessqlalchemyenginebase.py", line 962, in _execute_ddl
compiled = ddl.compile(dialect=dialect)
File "<string>", line 1, in <lambda>
File "C:Anaconda3libsite-packagessqlalchemysqlelements.py", line 494, in compile
return self._compiler(dialect, bind=bind, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlddl.py", line 26, in _compiler
return dialect.ddl_compiler(dialect, self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 190, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 213, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:Anaconda3libsite-packagessqlalchemysqlvisitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 2173, in visit_create_table
create.include_foreign_key_constraints)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 2220, in create_table_constraints
for constraint in constraints
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 2218, in <genexpr>
p for p in
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 2226, in <genexpr>
not getattr(constraint, 'use_alter', False)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 213, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:Anaconda3libsite-packagessqlalchemysqlvisitors.py", line 93, in _compiler_dispatch
return meth(self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 2369, in visit_check_constraint
literal_binds=True)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 213, in process
return obj._compiler_dispatch(self, **kwargs)
File "C:Anaconda3libsite-packagessqlalchemysqlvisitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 927, in visit_binary
return self._generate_generic_binary(binary, opstring, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 944, in _generate_generic_binary
binary.right._compiler_dispatch(self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlvisitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 527, in visit_grouping
return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
File "C:Anaconda3libsite-packagessqlalchemysqlvisitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 751, in visit_clauselist
for c in clauselist.clauses)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 748, in <genexpr>
s for s in
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 751, in <genexpr>
for c in clauselist.clauses)
File "C:Anaconda3libsite-packagessqlalchemysqlvisitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 1071, in visit_bindparam
bindparam, within_columns_clause=True, **kwargs)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 1103, in render_literal_bindparam
return self.render_literal_value(value, bindparam.type)
File "C:Anaconda3libsite-packagessqlalchemysqlcompiler.py", line 1118, in render_literal_value
return processor(value)
File "C:Anaconda3libsite-packagessqlalchemysqlsqltypes.py", line 171, in process
value = value.replace("'", "''")
File "C:Anaconda3libenum.py", line 268, in __getattr__
raise AttributeError(name) from None
AttributeError: replace
PEP-435枚举支持在1.1中添加。
1.1.0b1是最近发布的。你可以升级到1.1.0b1,但要小心bug。根据发布历史,我认为稳定版本应该在一两个月内发布。
如何升级:
pip install 'sqlalchemy==1.1.0b3'