如何在列名(和表名)中包含空格的数据库中使用sqlalchemy?
db.auth_stuff.filter("db.auth_stuff.first name"=='Joe')显然无法工作.我不想在进行反射时手动定义所有内容,而是要lambda x: x.replace(' ','_')在现有表名之间添加一些内容,这些内容正在从数据库中读取,并在我的模型中使用.(创建一个通用函数来重命名所有不能与python一起使用的表名也可能很有用 - 保留字等)
有这么简单/干净的方法吗?
我想我需要定义自己的mapper类?
https://groups.google.com/forum/#!msg/sqlalchemy/pE1ZfBlq56w/ErPcn1YYSJgJ
或者使用某种__mapper_args__参数 -
http://docs.sqlalchemy.org/en/rel_0_8/orm/mapper_config.html#naming-all-columns-with-a-prefix
理想情况下:
class NewBase(Base):
__mapper_args__ = {
'column_rename_function' : lambda x: x.replace(' ','_')
}
class User(NewBase):
__table__ = "user table"
}
Run Code Online (Sandbox Code Playgroud)