Hello, I am trying to port an outdated application to python with sqlalchemy.
The existing application database has about 300 tables, and each table has a class called def, for example:
create table accnt ( code varchar(20) , def varchar(50)
So, when with declarative syntax and reflection, I can easily create my class like:
class Accnt(Base): __table__ = Table('accnt', metadata, autoload = True, autoload_with=engine)
But when I try to find the def column, I end up getting an error. For instance:
q = session.query(Accnt) for row in q: print q.def
Since def is a reserved word for python: (
To overcome this problem, I can create my class as:
class Accnt(Base): __table__ = Table('accnt', metadata, autoload = True, autoload_with=engine) __mapper_args__ = {'column_prefix':'_'}
But putting _ in front of each column name is boring and not fantasizing.
What I would like to do is access the def column with a different name / (key?).
Any ideas?
--- Edit --- (Editing the original message at the request of TokenMacGuy )
While I accepted the TokenMacGuy answer, I tried it before:
engine = create_engine('firebird://sysdba: masterkey@127.0.0.1 /d:\\prj\\db2\\makki.fdb?charseββt=WIN1254', echo=False) metadata = MetaData() DbSession = sessionmaker(bind=engine) Base = declarative_base() class Accnt(Base): __table__ = Table('accnt', metadata, autoload = True, autoload_with=engine) _def = Column("def", String(50))
And I have sqlalchemy.exc.ArgumentError: cannot add extra column "def" when specifying table error ..
The main difference between me and TokenMacGuy is
mine : _table_ .... TokenMcGuy : __tablename__ = 'accnt' __table_args__ = {'autoload': True}
and metadata binding ...
So why did my previous attemp cause an error?