Julien Meyer <[email protected]> wrote:
> Hello, > > My question is linked to a previous question asked here > :https://groups.google.com/forum/#!topic/sqlalchemy/XUHmfrMRhdc. > > The context : Horizontal Sharding with dynamic shard (depending of the > execution context). > > All works fine for loading (with the solution given in the other thread) but > it fails for deletion. > > If I want to delete a parent instance (its shard id is know), the orm tries > to load the children items before (cascade delete). > To do it, a query is created from the Child mapper (LazyLoader._emit_lazyload > function). > On query execution, my shard session calls my "query_chooser" function. The > problem is that I need to got the shard_id from the parent object instance > and I didn't find it in the query object... > > I can solve my problem by overwritting the method "_emit_lazyload" in > LazyLoader class but I don't know if it is possible and how to do it… the state of an object (which you get from inspect(my_object)) stores that ShardId option inside a collection called load_options(). In your case, you should work things out so all of your objects have this option present 100% of the time. So supposing an object knows its shard on construction, we can set that up in an event: @event.listens_for(MyShardedClass, “init”) def init(target, args, kwargs): inspect(target).load_options += (ShardId(<the shard id>), ) In the ShardedQuery recipe, we have a “shard_id” in the context, let’s apply that also: @event.listens_for(MyShardedClass, “load”) def load(target, context): inspect(target).load_options += (ShardId(context.attributes[“shard_id”]), ) Basically, load_options is something we can get to in many ways, and this collection is consulted within all lazy loads. So I still think making sure that marker is present on the parent state in all cases is the best way to go. > > Thanks, > > Julien > > > > > > -- > 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 http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
