extend automapped classes with a mixin

283 views
Skip to first unread message

Imran Akbar

unread,
Oct 12, 2020, 1:59:36 PM10/12/20
to sqlalchemy

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 I call automapper.prepare() again, I get warnings like this and mostly just enters an infinite loop:

SAWarning: This declarative base already contains a class with the same class name and module name as sqlalchemy.ext.automap.payments, and will be replaced in the string-lookup table.

I cannot specify the database classes explicitly as in the documentation, because I don't know the database tables ahead of time (I'll be connecting to databases dynamically given a connection string).

Any thoughts?

thank you

Simon King

unread,
Oct 12, 2020, 2:29:37 PM10/12/20
to sqlal...@googlegroups.com
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

Imran Akbar

unread,
Oct 13, 2020, 12:28:33 PM10/13/20
to sqlalchemy
Simon, you are awesome!  That worked perfectly.

Thank you Sir.

Reply all
Reply to author
Forward
0 new messages