On Mon, Sep 20, 2021, at 5:04 AM, Nikola Radovanovic wrote:
> 
> Hi,
> When decoupling business from ORM model, dataclass passed to SA's imperative 
> mapper *map_imperatively* will receive SA's internals like for example 
> *_sa_instance_state* and *_sa_class_manager*.
> 
> I am wondering, what would be the best way to have "pure" dataclass, without 
> SA internals? First thing to come to my mind is to perform query on SA 
> *Table* and use dict/namedtuple to populate dataclass. Are there better 
> approaches? I am in particular interested how this approach will behave with 
> partial and composite updates? Any recipe to recommend?

to accomplish that you use a registry, typically a weak-keyed one, where keys 
are the classes / instances in use and the values are the class managers and 
states.   If you wanted to apply such a system to your classes you would make 
use of the instrumentation extension system: 

https://docs.sqlalchemy.org/en/14/orm/extensions/instrumentation.html

there's also an example of it :

https://docs.sqlalchemy.org/en/14/_modules/examples/custom_attributes/custom_management.html



now as far as if you *should* do this.  Absolutely not.  The instrumentation 
extension system is a very old and almost never used corner of the API which I 
moved out of the main library some years ago due to its complexity.   adding a 
weakref lookup to all class/state instrumentation lookups will add a palpable 
performance penalty to most ORM operations, and the instrumentation extension 
is likely not without bugs and regressions at this point.   it was first 
created over ten years ago to suit the use case of a single user who was 
working with Zope security proxied objects.    I've seen at least one other 
person using it, but overall i dont think it's worthwhile to spend lots of time 
going down tunnels like this; if your dataclass has a few attributes stuck on 
it, that shouldn't be a problem for any real-world situation.





> 
> Thank you in advance.
> 
> Kindest regards
> 
> 
> -- 
> 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/324223bc-695d-4479-be32-b1a7c73e8a76n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/324223bc-695d-4479-be32-b1a7c73e8a76n%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 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/297750d6-88e8-4475-af28-3591d30a6dc4%40www.fastmail.com.

Reply via email to