Query.get on base class raises IndexError with multiple table polymorphic inheritance

Issue #611 resolved
Former user created an issue

Using release 0.3.8. Base class Account, subclass MutualAcccount, using multiple table polymorphic inheritance. MutualAccount with primary key id=1 exists in the database.

>>> Query(MutualAccount).get(1)
>>> Query(Account).get(1)
Traceback (most recent call last):
  File "test.py", line 103, in <module>
    acct = Query(Account).get(account.id)
  File "/usr/local/lib/python2.5/site-packages/sqlalchemy/orm/query.py", line 103, in get
    return self._get(key, ident, **kwargs)
  File "/usr/local/lib/python2.5/site-packages/sqlalchemy/orm/query.py", line 944, in _get
    params[primary_key._label](primary_key._label) = ident[i](i)
IndexError: list index out of range

Not sure how to fix this, or whether my code is wrong somehow, or this is acceptable behaviour. Seems unreasonable to me. Attached is a test case.

Comments (1)

  1. Mike Bayer repo owner

    this bug was fixed in changeset:2636 for the 0.4 branch. when working with 0.3, you have two options. either name the primary key columns in the inherited table the same as the base table (as is commonly done for inheriting tables), or explicitly set the primary key on your base mapper, which propigates to the "select" mapper that works on your polymorphic_union (this was added in changeset:2626). your polymorphic union's primary key is 'account_id' due to the two distinctly named PK cols present.

    account_mapper = mapper(Account, account_table, select_table=account_join, primary_key=[account_table.c.id](account_table.c.id), polymorphic_on=account_join.c.account_type)
    
  2. Log in to comment