Sometimes it is useful to map a class against a join
instead of a single table when using SQLAlchemy's declarative extension. When column names collide, usually in a one-to-many because all primary keys are named id
by default, you can use .alias()
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.
For example:
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()
ST
has id
, fkey
properties with each name mapping to the first table in the join that uses the overridden name, so the mapped class does not expose t2
's primary key. ST2
has t1_id
, t2_id
and t2_fkey
properties.
Is there a convenient way to alias only some of the columns from each table in the join
so the mapped class exposes the more convenient non-prefixed property names for most mapped columns?