Query for non-primary mapper returns cached object for primary mapper

Issue #562 resolved
Former user created an issue

I use non-primary mapper with additional count field to gather statistics. This additional field is available when I query with clear session, but isn't when the object created using primary mapper exists in session cache (either just created and saved or selected with primary mapper).

Comments (1)

  1. Mike Bayer repo owner

    theres not really any way to avoid this behavior since a non-primary mapper is typically used to select rows in a different way, but not to add any new properties to a class. if you used entity_name mappers, the loaded classes would have an identity distinct from those already loaded, but in that case the alternate entity_name mapper is also responsible for re-persisting the instance so is again probably not what you want.

    to add a count field, the two easiest ways are either to create your primary mapper against, the select, or add the column at the query level via

    session.query(MyClass).add_column(func.count(whatever)).filter_by(foo).list()
    
  2. Log in to comment