title: SQLAlchemy declarative concrete autoloaded table inheritance
body: <p>I've an already existing database and want to access it using SQLAlchemy. Because, the database structure's managed by another piece of code (Django ORM, actually) and I don't want to repeat myself, describing every table structure, I'm using <code>autoload</code> introspection. I'm stuck with a simple concrete table inheritance.</p>
  
  <pre><code>Payment                FooPayment
    + id (PK) &lt;----FK------+ payment_ptr_id (PK)
    + user_id              + foo
    + amount
    + date
  </code></pre>
  
  <p>Here is the code, with table SQL descritions as docstrings:</p>
  
  <pre><code>class Payment(Base):
      """
      CREATE TABLE payments(
        id serial NOT NULL,
        user_id integer NOT NULL,
        amount numeric(11,2) NOT NULL,
        date timestamp with time zone NOT NULL,
        CONSTRAINT payment_pkey PRIMARY KEY (id),
        CONSTRAINT payment_user_id_fkey FOREIGN KEY (user_id)
            REFERENCES users (id) MATCH SIMPLE)
      """
      __tablename__ = 'payments'
      __table_args__ = {'autoload': True}
      # user = relation(User)
  
  class FooPayment(Payment):
      """
      CREATE TABLE payments_foo(
        payment_ptr_id integer NOT NULL,
        foo integer NOT NULL,
        CONSTRAINT payments_foo_pkey PRIMARY KEY (payment_ptr_id),
        CONSTRAINT payments_foo_payment_ptr_id_fkey
            FOREIGN KEY (payment_ptr_id)
            REFERENCES payments (id) MATCH SIMPLE)
      """
      __tablename__ = 'payments_foo'
      __table_args__ = {'autoload': True}
      __mapper_args__ = {'concrete': True}
  </code></pre>
  
  <p>The actual tables have additional columns, but this is completely irrelevant to the question, so in attempt to minimize the code I've simplified everything just to the core.</p>
  
  <p>The problem is, when I run this:</p>
  
  <pre><code>payment = session.query(FooPayment).filter(Payment.amount &gt;= 200.0).first()
  print payment.date
  </code></pre>
  
  <p>The resulting SQL is meaningless (note the lack of join condidion):</p>
  
  <pre><code>SELECT payments_foo.payment_ptr_id AS payments_foo_payment_ptr_id,
         ... /* More `payments_foo' columns and NO columns from `payments' */
      FROM payments_foo, payments
      WHERE payments.amount &gt;= 200.0 LIMIT 1 OFFSET 0
  </code></pre>
  
  <p>And when I'm trying to access <code>payment.date</code> I get the following error: <code>Concrete Mapper|FooPayment|payments_foo does not implement attribute u'date' at the instance level.</code></p>
  
  <p>I've tried adding implicit foreign key reference <code>id = Column('payment_ptr_id', Integer, ForeignKey('payments_payment.id'), primary_key=True)</code> to <code>FooPayment</code> without any success. Trying <code>print session.query(Payment).first().user</code> works (I've omited <code>User</code> class and commented the line) perfectly, so FK introspection works.</p>
  
  <p>How can I perform a simple query on <code>FooPayment</code> and access <code>Payment</code>'s values from resulting instance?</p>
  
  <p>I'm using SQLAlchemy 0.5.3, PostgreSQL 8.3, psycopg2 and Python 2.5.2.
  Thanks for any suggestions.</p>