-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to work with relationships? #79
Comments
I believe it's used to determine if models are allowed to be foreign keyed to eachother. So probably only used at either migrating creation time or at migration time. I can take a look later and point you to the relevant source code in Django. Support is meant to be limited to foreign keys on the same database. |
Here is my code: It is a bit messy. I have two shard groups from sharded_storage.models import Set
set1 = Set.objects.create()
set1.name = 'asdasd'
set1.save()
item1 = set1.items.create() raises an exception that it can't find |
I'm not sure about the specific error message but I can checkout your code and see. |
Are the items and set on the same database? Otherwise it's not meant to be an allowed relationship with an actual foreign key |
Ok, I think I got it. I need to change all relationships fields with But why wouldn't you extend Django relationships classes to allow those cross-shard links natively? |
Why do I need to define so much complicated def get_shard(self):
if self.pk is not None:
shard_group = getattr(self, 'django_sharding__shard_group')
django_sharding_app = apps.get_app_config('django_sharding')
bucketer = django_sharding_app.get_bucketer(shard_group)
return bucketer.get_shard(self)
else:
sharded_by_field = getattr(self, 'django_sharding__sharded_by_field')
sharded_by_field = self._meta.get_field(sharded_by_field)
self.pk = sharded_by_field.strategy.get_next_id()
return self.get_shard()
@staticmethod
def get_shard_from_id(pk):
if pk is not None:
shard_group = getattr(Set, 'django_sharding__shard_group')
django_sharding_app = apps.get_app_config('django_sharding')
bucketer = django_sharding_app.get_bucketer(shard_group)
obj = Set()
obj.pk = pk
return bucketer.get_shard(obj)
else:
sharded_by_field = getattr(Set, 'django_sharding__sharded_by_field')
sharded_by_field = Set._meta.get_field(sharded_by_field)
pk = sharded_by_field.strategy.get_next_id()
return Set.get_shard_from_id(pk) |
Django doesn't have good multi-db support as it is and I didn't really like the idea of hiding how the DB works from the code. It makes it harder to tweak DB usage, the more magic that happens. But yes, you'd use big integer fields instead. I'll take a look at this code you posted and your example. Is that code block also in your code? |
For the and |
Seems support of relationships is very bad. And it is hard to debug because
django_sharding_library.router.ShardedRouter.allow_relation
is never gets called.The text was updated successfully, but these errors were encountered: