Search code examples
djangogoogle-app-enginenosqldjango-nonreldjangoappengine

Django Nonrel - Working around multi-table inheritance with noSQL?


I'm working on a django-nonrel project running on Google's AppEngine. I want to create a model for a Game which contains details which are generally common to all sports - i.e. gametime, status, location, etc. I've then modelled specific classes for GameBasketball, GameBaseball etc, and these inherit from the base class.

This creates a problem however if I want to retrieve something like all the Games on a certain day:

Game.objects.filter(gametime=mydate)

This will return an error:

DatabaseError: Multi-table inheritance is not supported by non-relational DBs.

I understand that AppEngine doesn't support JOINs and so it makes sense that this fails. But I'm not sure how to properly tackle this problem in a non-relational environment. One solution I've tried is to turn Game into an abstract base class, and while that allows me to model the data in a nice way - it still doesn't resolve the use case above since its not possible to get objects for an abstract base class.

Is the only solution here to put all the data for all possible sports (and just leave fields that aren't relevant to a specific sport null) in the Game model, or is there a more elegant way to solve this problem?

EDIT: I'm more interested in understanding the correct way of handling this type of issue in any noSQL setup, and not specifically on AppEngine. So feel free to reply even if your answer isn't GAE specific!


Solution

  • The reason multi-table inheritance is disallowed in django-nonrel is because the API that Django provides for those sort of models will often use JOIN queries, as you know.

    But I think you can just manually set up the same thing Django would have done with it's model inheritance 'sugar' and just avoid doing the joins in your own code.

    eg

    class Game(models.Model):
        gametime = models.DateTimeField()
        # etc
    
    class GameBasketball(models.Model):
        game = models.OneToOneField(Game)
        basketball_specific_field = models.TextField()
    

    you'll need a bit of extra work when you create a new GameBasketball to also create the corresponding Game instance (you could try a custom manager class) but after that you can at least do what you want, eg

    qs = Game.objects.filter(gametime=mydate)
    qs[0].gamebasketball.basketball_specific_field
    

    django-nonrel and djangoappengine have a new home on GitHub: https://github.com/django-nonrel/

    I'm not convinced, next to the speed of GAE datastore API itself, that the choice of python framework makes much difference, or that django-nonrel is inherently slower than webapp2.