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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to