title: Is there a convenient way to alias only conflicting columns when joining tables in SQLAlchemy?
body: <p>Sometimes it is useful to map a class against a <code>join</code> instead of a single table when using <a href="http://www.sqlalchemy.org/" rel="nofollow noreferrer">SQLAlchemy</a>'s declarative extension. When column names collide, usually in a one-to-many because all primary keys are named <code>id</code> by default, you can use <code>.alias()</code> to prefix every column with its table name. That is inconvenient if you've already written code that assumes your mapped class has non-prefixed names.</p>
  
  <p>For example:</p>
  
  <pre><code>from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy import Table, Column, Integer, ForeignKeyConstraint
  
  Base = declarative_base()
  
  t1 = Table('t1', 
      Base.metadata, 
      Column('id', Integer, primary_key=True))
  
  t2 = Table('t2', 
      Base.metadata, 
      Column('id', Integer, primary_key=True),
      Column('fkey', Integer),
      ForeignKeyConstraint(['fkey'], [t1.c.id]))
  
  class ST(Base):
      __table__ = t1.join(t2)
  
  class ST2(Base):
      __table__ = t1.join(t2).alias()
  </code></pre>
  
  <p><code>ST</code> has <code>id</code>, <code>fkey</code> properties with each name mapping to the first table in the join that uses the overridden name, so the mapped class does not expose <code>t2</code>'s primary key. <code>ST2</code> has <code>t1_id</code>, <code>t2_id</code> and <code>t2_fkey</code> properties.</p>
  
  <p>Is there a convenient way to alias only some of the columns from each table in the <code>join</code> so the mapped class exposes the more convenient non-prefixed property names for most mapped columns?</p>