pythonsqlalchemy

Use alias for column name in SQLAlchemy


Suppose if I have a SQLAlchemy table as follows -

class Employee(Base):
    id = Column(Integer, primary_key=True)
    employee_desgination = Column(String)

I remember going through the docs once and seeing some way of using aliases for long column names and use shorter ones instead. For example, in the above table instead of calling Employee.employee_designation I'd like to use Employee.emp_d or something similar. I wasn't able to find the example again :/ I think you declare an alias() in the table definition, but I am not sure of the syntax.


Solution

  • You can specify the actual column name (if different from the attribute name) as the first argument to Column:

    emp_d = Column("employee_desgination", String)