On Oct 29, 8:44 pm, "Michael Bayer" <mike...@zzzcomputing.com> wrote:
> Tvrtko wrote:
>
> > Hi,
>
> > is it possible to merge an object back to session, but without merging
> > the entire tree of related objects. Just this one root object.
>
> the general way is to disable merge cascade on the relation(), using
> "cascade='save-update'" or "cascade=None"

I still didn't try "cascade" options, but the object copy works.

> > I created a new user instance and copied all relevant attributes to
> > it. I then tried to merge this copy back to session, but it doesn't
> > work because you can only merge detached objects not transient ones
>
> transient objects can be merged.  They get added to the session and enter
> the pending state.

I get:
InvalidRequestError: merge() with dont_load=True option does not
support objects transient (i.e. unpersisted) objects.  flush() all
changes on mapped instances before merging with dont_load=True.

I managed to make a copy for merge like this:

obj = User()
obj.user_id = user.user_id
obj.name = user.name
obj._instance_key = user.user_id
obj._state.modified = False
user = session.merge(obj, dont_load=True)

I guess I can now try the cascade option.

>
> > I then tried removing related objects but I'm not even sure how to do
> > that. Anyway, the problem here is that you can't merge dirty objects.
>
> dirty objects are also mergeable, but only if you allow the Session to
> load their existing state from the database (i.e. you dont use the
> 'dont_load' flag).   If you're playing with caching you shouldn't put
> dirty objects into a cache.

I need dont_load otherwise my caching isn't really caching.

>
> > My version of sqlalchemy is 0.4.4
>
> oof.   go to 0.4.8 at the very least.   0.5.6 preferably.

In the past, I would often have to spend days porting from one version
to another (0.2->0.3->0.4) because of some subtle changes in
sqlalchemy (sessions, threading, way of accessing properties,
mappers...). This would be just 0.4.x->0.4.y but I'm still reluctant.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to