title: SQLAlchemy: Getting a single object from joining multiple tables
body: <h1>DB - SQLAlchemy setting</h1>
  
  <p>Consider a classic setting of two tables - <code>user</code> and <code>api_key</code>, represented by SQLAlchemy objects as:</p>
  
  <pre><code>class User(Base):
      __tablename__ = 'user'
      user_id = Column(String)
      user_name = Column(String)
      vioozer_api_key = Column(String, ForeignKey("api_key.api_key"))
  
  class ApiKey(Base):
      __tablename__ = 'api_key'    
      api_key = Column(String(37), primary_key=True)
  </code></pre>
  
  <p>Other fields omitted for clarity.</p>
  
  <h1>My Query</h1>
  
  <p>Suppose that I want to get the user name and the api key for a specific user id:</p>
  
  <pre><code>user, api_key = database.db_session.query(User, ApiKey)\
      .join(ApiKey, User.vioozer_api_key==ApiKey.api_key)\
      .filter(User.user_id=='user_00000000000000000000000000000000').first()
  </code></pre>
  
  <p>I get two objects - <code>user</code> and <code>api_key</code>, from which I can fetch <code>user.name</code> and <code>api_key.api_key</code>.</p>
  
  <h1>The problem</h1>
  
  <p>I would like to wrap this call with a function, which would return a single objects whose fields would be the union of the <code>user</code> fields and the <code>api_key</code> fields - the same way a SQL <code>join</code> returns a table with the columns of both tables being joined. <strong>Is there a wayo to automatically get a object whose fields are the union of the fields of both tables?</strong></p>
  
  <h1>Wha have I tried</h1>
  
  <p>I can define a <a href="http://docs.sqlalchemy.org/en/rel_0_9/orm/mapper_config.html" rel="noreferrer">mapper class</a> for each Join operation, but I wonder if the mapping could be done automatically.</p>