Gah!  That's it, make_transient_to_detached does everything as you said.

I do truly only need "simple" loading on my objects, anything that has a 
many-to-many relationship (they're in 3 separate areas), which would 
require a separate dB call normally, I just set those manually.

Thanks Mike.

On Sunday, September 30, 2018 at 4:03:33 PM UTC+1, Mike Bayer wrote:

>
> it's cool with me whatever you want to do that works :)   as I noted 
>
 

> earlier, I do have mock test setups that don't use the DB at all, it's 
> just they're hard to implement and understand, but if you have one you 
> like then you're GTG.  However we have to clean up all that private 
> attribute stuff, as what you are doing there is still available 
> through public APIs. 
>
> Most of what you need you can get with make_transient_to_detached. 
> That will create your identity key, assign it, and the object is 
> addable to the Session as though you loaded it.  Here's the whole 
> thing and you don't need any of that stuff: 
>
> s = Session() 
>
> a1 = A(id=1) 
> make_transient_to_detached(a1) 
> s.add(a1) 
>
> b1 = B(a_id=1) 
> s.add(b1) 
> s.enable_relationship_loading(b1) 
> assert b1.a is a1 
>
>
> However, the reason I don't usually give this out as a real recipe, is 
> that it is extremely limited in its functionality.  It only works for 
> a simple many-to-one relationship with a default "primaryjoin" 
> condition.    Wont work for collections, many to many, complicated 
> joins, none of that.   If you truly only need simplistic many-to-one 
> loading, then that approach will be enough. 
>
 

-- 
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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to