On Tue, Jan 26, 2021, at 9:01 PM, Kent Bower wrote: > Thanks a ton for your responses. > > Do all the normal columns of an aliased class need to match the ad-hoc select > to which I map the alias?
handwavy handwavy sort of yes, sort of no? there needs to be a 1-1 correspondence of columns, yes, aliasedclass can't define additional attributes that arent there normally. as to how they are lined up, there's an option to do it by name in the aliased() function adapt_on_names. otherwise it's expected that your select() is derived from the Table objects that are originally mapped. > > > oh if the class doesn't have a mapper, then defintiely, just make ad-hoc > > subclasses of it and map to those. vastly easier that way. > > Mapping to a subclass would be different from mapping directly to an ad-hoc > class? if the class itself is ad-hoc, then you're fine, you can make new classes with mappers all you want and they are GCed if you lose them (assuming no relationships involving long-lived classes with backrefs), yes. > > > (Mostly I’m concerned whether the very act of adding a mapper to a class > will keep the reference and prevent its garbage collection or in some other > way modify the “main” mappers, especially if this is done via a thread. Like, > would that modify the compiled mappers for the entire process... these are > the things running through my head.) the configure_mappers() function these days is threadsafe, it uses a mutex, so you are "good" there but there's a mutex involved if you are looking for high levels of concurrency. if your ad-hoc classes are not subclasses of any long lived classes and if you are careful to limit relationships to only point to long lived classes and not have any backrefs, it should be OK. I'd test it though :) set up a weakref.ref() to your ad-hoc mapper objects and make sure those ref objects get their callback hook invoked. > > > On Tue, Jan 26, 2021 at 8:18 PM Mike Bayer <[email protected]> wrote: >> __ >> >> >> On Tue, Jan 26, 2021, at 7:31 PM, Kent Bower wrote: >>> I should have given these details from the get-go: the use case is a >>> specialized select() (dynamically built) which would be extremely >>> convenient to map relationships against for convenience in subquery >>> loading, etc. So, the class would not already have a mapper. Can I pass >>> non_primary=True anyway, or won’t this work? >> >> oh if the class doesn't have a mapper, then defintiely, just make ad-hoc >> subclasses of it and map to those. vastly easier that way. >> >> >> >>> >>> >>> >>> On Tue, Jan 26, 2021 at 6:18 PM Mike Bayer <[email protected]> wrote: >>>> __ >>>> >>>> >>>> On Tue, Jan 26, 2021, at 3:16 PM, Kent wrote: >>>>> Question: if I add a mapper to a class that is only needed temporarily, >>>>> does using the mapper compile it along side my "normal" mappers such that >>>>> I'll leak memory when I mean for the class to be garbage collected? >>>>> >>>>> Put another way, can I add a mapper to a class that doesn't influence my >>>>> "main mappers" and gets thrown away when the class is thrown away or is >>>>> that not possible? >>>> >>>> Assuming the class already has a mapper, and this is an additional mapper >>>> that maps the class in a different way, this feature is called a >>>> "non-primary mapper" and requires that you pass the non_primary=True flag. >>>> If you are using non-primary mappers, then yes they get garbage >>>> collected. >>>> >>>> This is also a deprecated feature that won't be in SQLAlchemy 2.0. The >>>> only use case we can identify for "non-primary mappers" is when you want >>>> to create a relationship() to one, and for that use case we now support >>>> the "Relationship to AliasedClass" pattern. >>>> >>>> For anything else involving retrieving a class from an alternate >>>> selectable of some kind, you should be able to use AliasedClass for, e.g. >>>> sqlalchemy.orm.aliased(), so I would recommend using that instead unless >>>> you are on a very old version of SQLAlchemy. >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>>> Thanks in advance, >>>>> Kent >>>>> >>>>> -- >>>>> 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 [email protected]. >>>>> To view this discussion on the web visit >>>>> https://groups.google.com/d/msgid/sqlalchemy/df03ce24-3986-450c-8c3a-a020d5d6adccn%40googlegroups.com >>>>> >>>>> <https://groups.google.com/d/msgid/sqlalchemy/df03ce24-3986-450c-8c3a-a020d5d6adccn%40googlegroups.com?utm_medium=email&utm_source=footer>. >>>> >>>> >>>> -- >>>> 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 a topic in the >>>> Google Groups "sqlalchemy" group. >>>> To unsubscribe from this topic, visit >>>> https://groups.google.com/d/topic/sqlalchemy/IAqOQFpiB20/unsubscribe. >>>> To unsubscribe from this group and all its topics, send an email to >>>> [email protected]. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/sqlalchemy/c8f7f84e-af90-450c-9289-ed84fc6996a9%40www.fastmail.com >>>> >>>> <https://groups.google.com/d/msgid/sqlalchemy/c8f7f84e-af90-450c-9289-ed84fc6996a9%40www.fastmail.com?utm_medium=email&utm_source=footer>. >>> >>> -- >>> 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 [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/sqlalchemy/CANnF6aEAcuCRJrAQDWYrDGVmv5k7OuXNC_AXgrqDO00L3j%3D%2BAg%40mail.gmail.com >>> >>> <https://groups.google.com/d/msgid/sqlalchemy/CANnF6aEAcuCRJrAQDWYrDGVmv5k7OuXNC_AXgrqDO00L3j%3D%2BAg%40mail.gmail.com?utm_medium=email&utm_source=footer>. >> >> >> -- >> 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 a topic in the >> Google Groups "sqlalchemy" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/sqlalchemy/IAqOQFpiB20/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/sqlalchemy/dc49d8ba-ee87-4284-a0fd-140471edc1cb%40www.fastmail.com >> >> <https://groups.google.com/d/msgid/sqlalchemy/dc49d8ba-ee87-4284-a0fd-140471edc1cb%40www.fastmail.com?utm_medium=email&utm_source=footer>. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/CANnF6aEooQ2Hz7XenTpT_eP2vrsUpt%3DRvmwRtcuRuy4U%2BNKr1g%40mail.gmail.com > > <https://groups.google.com/d/msgid/sqlalchemy/CANnF6aEooQ2Hz7XenTpT_eP2vrsUpt%3DRvmwRtcuRuy4U%2BNKr1g%40mail.gmail.com?utm_medium=email&utm_source=footer>. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/265ce0d8-8fe7-4a25-8a3a-c728d3fb06de%40www.fastmail.com.
