On Mar 25, 2008, at 9:02 PM, Kipb wrote:

> Can you confirm that, for your database, column names with spaces are
> automatically quoted (no quote=True required) and that Column's key=
> works to give a python-identifier name to that column name (no mapper
> properties required)?

quote=True should not be needed, that is automatic....this stuff  
should work for MS-SQL as well though I dont have a test environment  
here for MS-SQL  Heres a sqlite script illustrating both concepts in  
action:

from sqlalchemy import *
from sqlalchemy.orm import *

engine = create_engine('sqlite://', echo=True)

meta = MetaData(engine)

t1 = Table('some table', meta,
         Column('primary col', Integer, key='id', primary_key=True),
         Column('the data', String(50), key='data')
         )

t1.create()

class Foo(object):
     def __eq__(self, f):
         return f.id == self.id and f.data == self.data

mapper(Foo, t1)

sess = create_session()

f = Foo()
f.data = "some data"

sess.save(f)
sess.flush()
sess.clear()

assert sess.query(Foo).filter_by(id=f.id).one() == f

> Any other illumination?  I'd love to have a function auto-generate the
> "column name without spaces" identifier that I use in my code, too,
> for even less Repeating Myself.

do something like this, using column overrides at the mapper level:

from sqlalchemy.orm import mapper as sa_mapper

def mapper(cls, tbl, **kwargs):
     for c in tbl.c:
         key = c.name.replace(' ', '_')
         kwargs[key] = c
     return sa_mapper(cls, tbl, **kwargs)

to do it at the Table level, I'd build a similar function that  
decorates Column to manufacture a "key" argument.  If you were using  
reflection (i.e. autoload=True), thats a little more tricky, it might  
require copying the Table to a new one to do it in an "automated"  
fashion.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to