Re: [sqlalchemy] Re: Can I make bulk update through association proxy?

2016-02-26 Thread Jonathan Vanasco


On Friday, February 26, 2016 at 4:54:01 AM UTC-5, Simon King wrote:
>
>
> I'm not aware of any way to do this.
>
>
I'm not either, and I'm scared of the SQL that would be generated and the 
wire traffic/memory if there were.  That would be subselects within 
subqueryloads within... this also seems a bit more about "updates to 
associations" vs "association_proxy".  

Just to step back for a second, calling `update()` on the relation would 
require every object to be loaded from the database and into the identity 
map.  This is for 2 reasons:  1) the ORM works on objects, not the db 
directly; 2) sqlalchemy would need to keep both objects in the   database 
and in-memory in sync (what if the 10% of the objects are loaded from 
memory and the rest are in the DB?).  Think of how that feature would work 
on an object where a relation has 1MM rows in the association - the 
computer could grind to a halt.

For this general task, I `flush` the session, use the `update` command on 
the target class -- filtering the WHERE based on the parent object and 
 join conditions --  then I `expire_all` (because that update may have 
affected in-memory object relations).  There is a small hit on reloading 
all the data, but I've found the `update` to run considerably faster and 
make it worth-it.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: Can I make bulk update through association proxy?

2016-02-26 Thread Piotr Dobrogost
On Thursday, February 25, 2016 at 2:33:13 PM UTC+1, Simon King wrote:

Maybe I'm not understanding your question properly. The return value from 
> query.all() is a plain python list. You're asking for it to return a 
> different kind of object, that wraps the underlying list and allows you to 
> specify arbitrary operations that should be applied to each object in that 
> list? I guess I could imagine a complicated class that might support 
> something like that, but I don't think it exists at the moment, and it 
> would seem like a lot of work just to avoid a simple loop...
>

Well, after calling .all() you indeed get plain python list and you can't 
do anything but iterate over it; it's "too late". In this case it appears 
one should not call .all() and instead call something on the query itself 
i.e. session.query(Text).???.update(...). I would like to know if and what 
to insert so that SA would generate UPDATE for objects _related_ (those 
accessible via association proxy) to these selected objects.

Regards,
Piotr

>

-- 
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.
For more options, visit https://groups.google.com/d/optout.