On Fri, Jul 5, 2019, at 6:16 AM, Simon King wrote:
> In Python, modules are executed when they are imported. Class
> definitions are executable statements, where the metaclass is called
> to construct the *class*. When you import a module containing
> declarative classes, the SQLAlchemy metaclass is called for each of
> those classes, building the mappings, registering the polymorphic
> identities and so on.


The pattern from a software architecture point of view was identified by Martin 
Fowler as the "registry" pattern, 
https://martinfowler.com/eaaCatalog/registry.html, however this pattern doesn't 
say much about the mechanics used. The registry pattern refers to linking the 
creation of a class or object to its automatic inclusion in some semi-global or 
in some cases global registry, which is one SQLAlchemy uses a lot, in some 
cases explicitly and in other cases only behind the scenes.

The declarative metaclass contains a function which scans your class for 
attributes, builds a Table from these attributes which is associated with the 
MetaData collection that the metaclass has access towards (this is registry 
pattern #1). Then, the mapper() function is invoked against the class you 
created along with this Table, which creates a new Mapper object that is added 
to a global, weak-referencing collection inside the sqlalchemy.orm.mapper 
module (this is registry pattern #2). When the mapper "inherits" from another 
one as seems to be the case here, that inheriting mapper is also amended to 
include this new mapper in its collection of subclasses.

The overall "mapper configure" step which you may have seen scans through this 
registry of mapper objects and makes sure all the mappers that refer to each 
other, usually through the relationship() linkage, are fully linked and have 
been found.




> 
> Simon
> 
> On Fri, Jul 5, 2019 at 10:58 AM natsjoo sodillepa <snats...@gmail.com> wrote:
> >
> > For the curious. One thing that lingers around is the question: how does 
> > SQLalchemy make the connection between the parent and the child classes?
> >
> > In the separate modules, where the child classes are defined there is no 
> > other code. The only thing that gets executed is the class construction, 
> > but as
> > far as I know that doesn't trigger execution of code in the parent class. I 
> > know that SQLalchemy uses a metaclass but that's used only for
> > instantiation of instances, not of classes, or is it?
> >
> > --
> > 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 post to this group, send email to sqlalchemy@googlegroups.com.
> > Visit this group at https://groups.google.com/group/sqlalchemy.
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msgid/sqlalchemy/c90cafee-13a4-4759-99a7-b2fd656697b2%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> 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 post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/CAFHwexebd5F9HXSX_kcBCY3Xey9aiPLThLX3h6GD1v7bomXd%3Dg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
> 

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/199d9628-bb4a-48bb-9c5a-cb88df2124cc%40www.fastmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to