好吧,我想到的是:
根据定义自定义列类型
http://www.sqlalchemy.org/docs/reference/sqlalchemy/types.html#custom-types
文档和一些试用版的结合;错误告诉我:
class MyDuckType(sqlalchemy.types.TypeDecorator):
"""
SQLALchemy custom column type, designed to let sqlite handle the typing
using 'numeric affinity' which intelligently handles both numbers and strings
"""
impl = sqlite.NUMERIC
def bind_processor(self, dialect):
#function for type coercion during db write
return None #ie pass value as-is, let sqlite do the typing
def result_processor(self, dialect, coltype):
#function for type coercion during db read
return None #ie pass value as sqlite has stored it, should be ducktyped already
def process_bind_param(self, value, dialect):
#any changes to an individual value before store in DN
return value
def process_result_value(self, value, dialect):
#any changes to an individual value after retrieve from DB
return value
def copy(self):
#not quite sure what this is for
return MyDuckType()
当前的sqlalchemy方言类型返回到bind\u处理器中的\u float,这就是我之前得到错误的原因。i、 m.v.v.h.o.,这是个虫子。
对于我的加分:在my metadata.reflect()代码中将列类型手动设置为MyDuckType:
def get_database_tables(engine):
meta = MetaData()
meta.reflect(bind=engine)
tables = meta.raw_tables
for tbl in tables.values():
for col in tbl.c:
col.type = MyDuckType()
return tables
似乎对我有用。有什么建议/改进吗?