On Dec 1, 2007, at 11:10 PM, Simon Wittber wrote:
> > I'd like to set up a sharding strategy where shards are chosen based > on this code: > > SHARD_COUNT = 5 > > databases = dict((i,create_engine('sqlite://shard_%s.db' % i)) for i > in xrange(SHARD_COUNT)) > > def shard_chooser(mapper, instance, clause=None): > return instance.primary_key_id % SHARD_COUNT > > Ie, I have 5 shards, and I choose the shard based on primary_key % 5. > > To do this, I need to ensure primary keys are globally unique, so I > use a function similar to the id_generator function in the shard > example here: > > http://www.sqlalchemy.org/trac/browser/sqlalchemy/trunk/examples/sharding/attribute_shard.py > > and use that function in my Table definitions with the 'default' named > argument. > > The problem with this approach, is that the shard_chooser function is > called before the primary_key is created and set on the instance, and > therefore raises a TypeError. > > return instance.user_id % SHARD_COUNT > TypeError: unsupported operand type(s) for %: 'NoneType' and 'int' > > Is there any way around this issue, other than explicitly calling an > id_generator function, and setting the primary key before the instance > gets passed to the shard chooser? I would call id_generator() *within* shard_chooser() if the pk is not available, that way shard_chooser is guaranteed to have the info that it needs. other options include MapperExtension.before_insert() but I dont see that as any more convenient than just within shard_chooser. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---