How can I use sqlalchemy on a database where the column names (and table names) have spaces in them?
db.auth_stuff.filter("db.auth_stuff.first name"=='Joe')
obviously can't work. Rather than manually define everything when doing the reflections I want to put something like lambda x: x.replace(' ','_')
between existing table names being read from the db, and being used in my models. (It might also be useful to create a general function to rename all table names that won't work well with python - reserved words etc.)
Is there an easy/clean way of doing this?
I think I need to define my own mapper class?
https://groups.google.com/forum/#!msg/sqlalchemy/pE1ZfBlq56w/ErPcn1YYSJgJ
Or use some sort of __mapper_args__
parameter -
http://docs.sqlalchemy.org/en/rel_0_8/orm/mapper_config.html#naming-all-columns-with-a-prefix
ideally:
class NewBase(Base):
__mapper_args__ = {
'column_rename_function' : lambda x: x.replace(' ','_')
}
class User(NewBase):
__table__ = "user table"
}
See Question&Answers more detail:os