On Dec 6, 2010, at 3:29 PM, Daniel Holth wrote: > I am trying to subclass a mapped class from another package for no > other reason than to add a utility method. The polymorphic_on > condition, if it were defined, would have to be 'True'. Example: > > class Mapped(declarative_base()): > # columns > > class Utility(Mapped): > def is_something(self): > return 'foo' > > Encouraged by the fact that session.query(Utility).first() works, I > proceeded and got an exception. > > The error is > > FlushError: Attempting to flush an item of type <Utility> on > collection 'Something.mapped', which is not the expected type. > Configure mapper 'Mapper|User|user' to load this subtype > polymorphically, or set enable_typechecks=False to allow subtypes. > Mismatched typeloading may cause bi-directional relationships > (backrefs) to not function properly. > > I'm starting to think I should just monkey patch the original class or > just pass the Mapped() into utility()... is that the best way?
That error is illustrating that while you can make a Utility(Mapped) object easily enough, when you later say session.query(Mapped), you're going to get Mapped objects, not Utility objects, back. Compounding the issue is that by using declarative, Utility is implicitly mapped. If you were using classical mappings with the mapper() function, you could get away with Utility not being mapped by itself, and things would "work", except that you would not get "Utility" objects back from query(Mapped), or even query(Utility). So you'd need to decide here what behavior you'd want here, though if there is no option other than using declarative and you don't want "utility" to be expressly represented in the database via polymorphic discriminator, you'd likely need to append your class to Mapped.__bases__ . -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
