Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:37:33+00:00 2026-05-26T02:37:33+00:00

Hello I’m trying to port a legacy application to python with sqlalchemy. The application’s

  • 0

Hello I’m trying to port a legacy application to python with sqlalchemy.

The application’s existing database has about 300 tables and in every table there is a colum named def such as :

create table accnt (
    code varchar(20)
  , def varchar(50) --for accnt definition
  , ...
)

So when with declarative syntax and reflection I can easily create my class as :

class Accnt(Base):
    __table__ = Table('accnt', metadata, autoload = True, autoload_with=engine)

But when I try to reach def column I eventually get an error. For example :

q = session.query(Accnt)
for row in q:
    print q.def

Because def is a reserved word for python 🙁

To overcome this issue I can create my class as :

class Accnt(Base):
    __table__ = Table('accnt', metadata, autoload = True, autoload_with=engine)
    __mapper_args__ = {'column_prefix':'_'}

But putting a _ in front of every column name is boring and not fancy.

What I’d like to do is access def column with another name / ( key ?).

Any ideas?

— Edit —
( Editing original post as requested by TokenMacGuy )

While I’ve accepted TokenMacGuy‘s answer I’ve tried it before as :

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’ve got
sqlalchemy.exc.ArgumentError: Can’t add additional 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 my previous attemp generated an error ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:37:33+00:00Added an answer on May 26, 2026 at 2:37 am

    You can have your cake and eat it too. Define the columns you want to rename; sqlalchemy will automatically infer any columns you don’t mention.

    >>> from sqlalchemy import *
    >>> from sqlalchemy.ext.declarative import declarative_base
    >>> 
    >>> engine = create_engine("sqlite:///:memory:")
    >>> 
    >>> engine.execute("""
    ... create table accnt (
    ...     id integer primary key,
    ...     code varchar(20),
    ...     def varchar(50)
    ... )
    ... """)
    <sqlalchemy.engine.base.ResultProxy object at 0x2122750>
    >>> 
    >>> Base = declarative_base()
    >>> 
    >>> Base.metadata.bind = engine
    >>> 
    >>> class Accnt(Base):
    ...     __tablename__ = 'accnt'
    ...     __table_args__ = {'autoload': True}
    ...     def_ = Column('def', String)
    ... 
    >>> Accnt.def_
    <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x2122e90>
    >>> Accnt.code
    <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x2127090>
    >>> 
    

    EDIT:

    By supplying a __table__ argument, you’re telling the declarative extension that you already have a properly configured Table that you’d like to use. But that’s not true; you want to have the def column referred to by another name in the class. By using __tablename__ and __table_args__, you defer the construction of the table until after you’ve told declarative how you want to use that table. There’s no elegant work-around if you are dead set on using __table__. You can provide a property that aliases the column or you may be able to specify the column as _def = getattr(__table__.c, 'def').

    Really, you should just use __tablename__; It’s both more convenient and more flexible, and this is a great example of why.

    (as an aside, it’s most conventional to give alternate identifiers a trailing underscore instead of a leading underscore, use def_ instead of _def; leading underscores usually signify that the name is ‘private’ or ‘an implementation detail’, if the name is meant to be public, but looks like a private name, it may cause more confusion than is necessary)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello every one. I'm faced with a weird problem. My application has a simple
Hello I'm a trying to learn python, In C++ to read in string from
Hello everyone I'm trying to make a flash uploader. The issue however that has
Hello i am trying to implement a database-object(connection) pooler for BerkeleyDB... I decided to
Hello we have an SQL server application running over a low bandwith connection. We
Hello Stack Overflow contributers, I'm a novice programmer learning Python right now, and I
Hello I'm about to create a school management system where it should assure the
Hello guys i have an website online wich access the database. The database access
Hello I'm making RSS reader and I'm using DOM. Now I stuck, trying to
Hello i been trying to get a tokenizer to work using the boost library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.