Вам необходимо сопоставить select()
с желаемыми столбцами. Отображение join()
заставляет sqlalchemy превратить его в соответствующий select * from <join>
:
(пропустите этот блок: это просто создает тестовое устройство)
>>> from sqlalchemy.orm import mapper, sessionmaker
>>> from sqlalchemy.sql import join, select
>>> from sqlalchemy import *
>>> # engine = create_engine("postgresql://<username>:@localhost/<DBname>")
... engine = create_engine("sqlite:///:memory:")
>>> engine.execute(r"""
... CREATE TABLE userprofile (
... id integer primary key,
... fieldid integer,
... keep integer,
... discard integer
... )
... """)
<sqlalchemy.engine.base.ResultProxy object at 0x2390d90>
>>> engine.execute(r"""
... CREATE TABLE profilefield (
... id integer primary key,
... fieldid integer,
... keep integer,
... discard integer
... )
... """)
<sqlalchemy.engine.base.ResultProxy object at 0x2390e90>
>>> metadata = MetaData(engine)
>>> Session = sessionmaker(engine)
>>> profiles = Table('userprofile',metadata,autoload=True)
>>> profilefields = Table('profilefield',metadata,autoload=True)
>>> engine.execute(profiles.insert({'fieldid': 1, 'keep': 2, 'discard': 3}))
<sqlalchemy.engine.base.ResultProxy object at 0x23e9750>
>>> engine.execute(profilefields.insert({'fieldid': 1, 'keep': 2, 'discard': 3}))
<sqlalchemy.engine.base.ResultProxy object at 0x23e98d0>
>>> class DBObj(object):
... def __repr__(self):
... return "DBobj" + str(self.__dict__)
...
>>> j = join(profiles,
... profilefields,
... profiles.c.fieldid==profilefields.c.fieldid)
Вот новая часть:
>>> js = select([profiles.c.id,
... profiles.c.keep,
... profilefields.c.keep,
... profiles.c.fieldid,
... profilefields.c.fieldid],
... use_labels=True,
... from_obj=j)
>>> mapper(DBObj,alias(js),properties={'fieldid':[profiles.c.fieldid,profilefields.c.fieldid]})
<Mapper at 0x23e9ad0; DBObj>
>>> q = Session().query(DBObj).all()
>>> q
[DBobj{u'userprofile_keep': 2, u'userprofile_id': 1, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x23f08d0>, 'fieldid': 1, u'profilefield_keep': 2}]