Dear everyone, On Thu, Aug 11, 2016 at 11:08 AM Gombas, Gabor < gabor.gom...@morganstanley.com> wrote:
> Well, it is somewhat similar to my use case: there is a part (kind of a > library) of the application which may load an object or it may > transparently create one if it did not exist before. And there is a > completely different part of the application which wants to do so something > only if the object was freshly created (because it would be too expensive > to do unconditionally) - but it has no idea where the object is coming > from. What I've done is adding an attribute which gets set to True in > __init__() and False by the @reconstructor - a more complete implementation > would probably want to reset the flag to False on objects which are alive > when a transaction commits, but my use case does not need that complexity. > I have not looked at @reconstructor before, thanks for pointing it out :). On Thu, Aug 11, 2016 at 6:13 PM Mike Bayer <mike...@zzzcomputing.com> wrote: > Like other replies here I'm not understanding the link between "commit" > and "get_or_create". Having a dictionary of objects that already > exists is a fine pattern to use for get_or_create but if you have the > object and a primary key, it's in. If this is a collection that is used > concurrently by many sessions, then I'd build a session-local copy of > your object map and then push it out to the "global" one within the > after_commit() event. The requirement mainly boils down to the container/contained status of the object and that I need to flag them as preexisting if they already exist in the database permanently. This is where I need the flush/commit distinction: I will always need to flush first to get the object's IDs (so I can calculate its container unique ID and determine whether the container already exists or not). However at this point, looking at the 'persistent' status alone to determine whether an object is preexisting or not is not enough since it will return True for my newly flushed object and my previously committed records in the database. I can't then flag the object appropriately. `get_or_create` was simply the function where I need to do this. I explained it hoping that it would give more context, but I see now that maybe I should've kept it to `commit` vs `flush` status :). But anyway, thanks for the explanation on the local-session copy, Mike. I think that would also be good for me to solve my problem. Best regards, Bow -- 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.