this is normal, loading for the base class only hits those columns which are 
defined for that base class - it does not automatically fan out to all columns 
mapped by all subclasses.

to do so, you can specify with_polymorphic:

c = s.query(with_polymorphic(PolyBase, [Cake])).filter_by(name='c1').one()

or for all subclasses:

c = s.query(with_polymorphic(PolyBase, "*")).filter_by(name='c1').one()


the current docs here tend to be centric towards joined-table loading and talk 
about "which tables are loaded", but it's really "which columns":

http://docs.sqlalchemy.org/en/rel_0_8/orm/inheritance.html?highlight=with_polymorphic#sqlalchemy.orm.with_polymorphic
http://docs.sqlalchemy.org/en/rel_0_8/orm/inheritance.html?highlight=with_polymorphic#basic-control-of-which-tables-are-queried




On Sep 20, 2013, at 11:08 AM, Philip Scott <[email protected]> wrote:

> Hello again dear list,
> 
> I have a mapped class is polymorphic with single table inheritance. I have a 
> function wired up to the load() SA event. The dictionary in the InstanceState 
> which is passed to the load event depends on whether I am querying on the 
> base class or a derived one, which I don't _think_ it should be.
> 
> Here is a concrete example, which is a bit contrived - I am actually knee 
> deep inside mapping mutable JSON objects stored in values of HSTOREs; this is 
> a distilled version of the problem:
> 
> class PolyBase(Base):
>     name            = Column(String, primary_key=True)
>     type            = Column(String)    
>     __mapper_args__ = {'polymorphic_on': type}
> 
> class Cake(PolyBase):
>     topping         = Column(String)
>     __mapper_args__ = {'polymorphic_identity': 'Cake'}
> 
> def load_event(state, *args):
>     print state.dict.keys()
> 
> sqlalchemy.event.listen(Cake, 'load', load_event, raw=True, propagate=True)
> 
> Now:
> 
> > session().query(Cake).filter_by(name='mycake').one()
> ['date', '_sa_instance_state', 'type', 'name']
> 
> However, in a fresh session:
> 
> > session().query(PolyBase).filter_by(name='mycake').one()
> ['_sa_instance_state', 'type', 'name']
> 
> See 'date' is missing when I query on PolyBase, even though in both cases 
> state.obj is a Cake instance. I've been looking at the object loading code in 
> SQLAlchemy and it is rather fierce; is this expected behavior?
> 
> Either way can anyone think of a fix/workaround?
> 
> Cheers for reading this far!
> 
> - Philip
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to