python - sqlalchemy - reflecting tables and columns with spaces -


how can use sqlalchemy on database column names (and table names) have spaces in them?

db.auth_stuff.filter("db.auth_stuff.first name"=='joe') can't work. rather manually define when doing reflections want put lambda x: x.replace(' ','_') between existing table names being read db, , being used in models. (it might useful create general function rename table names won't work python - reserved words etc.)

is there easy/clean way of doing this?

i think need define own mapper class?

https://groups.google.com/forum/#!msg/sqlalchemy/pe1zfblq56w/erpcn1yysjgj

or use 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"     } 

you can using reflection event give columns .key, full recipe has bug when primary key columns involved, fixed in still-unreleased 0.8.3 version (as master). if check out 0.8.3 @ https://bitbucket.org/zzzeek/sqlalchemy/get/rel_0_8.zip recipe work primary key cols:

from sqlalchemy import * sqlalchemy.orm import * sqlalchemy.ext.declarative import declarative_base, deferredreflection  base = declarative_base(cls=deferredreflection)   e = create_engine("sqlite://", echo=true) e.execute("""     create table "user table" (             "id col" integer primary key,             "data col" varchar(30)     ) """)  sqlalchemy import event  @event.listens_for(table, "column_reflect") def reflect_col(inspector, table, column_info):     column_info['key'] = column_info['name'].replace(' ', '_')  class user(base):     __tablename__ = "user table"  base.prepare(e)  s = session(e) print s.query(user).filter(user.data_col == "some data") 

deferredreflection optional helper use declarative + reflection.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -