Simon, you are awesome! That worked perfectly. Thank you Sir.
On Monday, October 12, 2020 at 11:29:37 AM UTC-7 Simon King wrote: > On Mon, Oct 12, 2020 at 6:59 PM Imran Akbar <skun...@gmail.com> wrote: > > > > Hi, > > > > I'm using SQLAlchemy and am generating classes dynamically for my > database via the Automapping functionality. > > > > I need to add a Mixin class with various helper methods to each of these > automapped classes. > > > > I tried to create subclasses of the automapped class with the mixin > class: > > > > db = create_engine(connection_string) > > automapper = automap_base() > > automapper.prepare(db, reflect=True) > > for class_variable in automapper.__subclasses__(): > > new_class = type(class_variable.__name__, (class_variable, Mixins), {}) > > > > but when I try to use these classes I get errors like: > > > > class _ is a subclass of AutomapBase. Mappings are not produced until > the .prepare() method is called on the class hierarchy. > > > > If you want to add your methods to *every* automapped class, you can > change the base class used by automap_base(). Something like this: > > class MySpecialBaseClass(object): > # define your helper methods here > pass > > automapper = automap_base(cls=MySpecialBaseClass) > > This works because automap_base passes most of its arguments on to > declarative_base: > > > https://docs.sqlalchemy.org/en/13/orm/extensions/automap.html#sqlalchemy.ext.automap.automap_base > > ...and declarative_base accepts a "cls" argument which is used as the > base class: > > > https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/api.html#sqlalchemy.ext.declarative.declarative_base > > Hope that helps, > > Simon > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- 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 sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/980ac90b-00fd-40bf-a7b3-b4d863267e1en%40googlegroups.com.