Re: [sqlalchemy] extend automapped classes with a mixin

2020-10-13 Thread Imran Akbar
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  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.


Re: [sqlalchemy] extend automapped classes with a mixin

2020-10-12 Thread Simon King
On Mon, Oct 12, 2020 at 6:59 PM Imran Akbar  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/CAFHwexfK%2BnjPge%3DZB4f1vR8paFAbaFtyeA-GKM9BFQYQosHn0w%40mail.gmail.com.


[sqlalchemy] extend automapped classes with a mixin

2020-10-12 Thread Imran Akbar


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

-- 
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/6f87255c-52a9-4998-a012-2e43a538fb7en%40googlegroups.com.