> 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.
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:
>
> 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:
>
>
> Where are the default Dialect quoting rules pertaining to spaces?
> I'm presuming in sql/compiler.py:IdentifierPreparer, but I don't see
> the regexp expressions for spaces there.....
the regexp is compiler.LEGAL_CHARACTERS, and is used within
IdentifierPreparer._requires_quotes.
> As far as I know, the MSSQL Dialect doesn't override the normal
> quoting rules, but the Dialect interface has changed since I last
> spent any significant time working with it, so I'm not really all
> that sure about it.
can you try my test script with an MS-SQL database and see what you
get ?
can you try my test script with an MS-SQL database and see what you
get ?