4

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!

2
  • This is not an answare, just an advice: Try to migrate to webapp2. django-norel is not supported any more and apart of that, its much slower. You will be paying far more resources on appengine. Commented Jun 19, 2012 at 16:01
  • Appreciate the advice - I'm actually planning to move off of AppEngine altogether before this project goes live, but I'll be sticking with some sort of noSQL solution (likely MongoDB). I'm more interested in the broader question of how to correctly model this type of data structure in a non-relational environment, even if the answer isn't specific to GAE or MongoDB.
    – BlueMoon
    Commented Jun 19, 2012 at 18:11

2 Answers 2

1

For anyone else who has this issue in the future, here is how I eventually went about resolving it. Keep in mind that this is somewhat of a mongo-specific solution, and AFAIK there is no easy way to solve this just using base nonrel classes.

For Mongo I able to solve this by using Embedded Documents (other noSQL setups may have similar soft-schema type features), simplified code below:

class Game(models.Model):
    gametime = models.DateTimeField()
    # etc

    details = EmbeddedModelField() # This is the mongo specific bit.

class GameBasketballDetails(models.Model):
    home = models.ForeignKey(Team)
    # etc

Now when I instantiate the Game class I save a GameBasketballDetails object in the details field. So I can now do:

games = Game.objects.filter(gametime=mydate)
games[0].details.basketball_specific_method()

This is an acceptable solution since it allows me to query on the Game model but still get the "child" specific info I need!

0

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.

3
  • My understanding was that OneToOneField(s) are not supported with nonrel. If that has changed I think the docs seem to still indicate that (see allbuttonspressed.com/blog/django/2010/01/… ). I've come up with a decent solution I'm posting below, but will go ahead and mark this as correct in case I'm wrong on OnetoOneField support!
    – BlueMoon
    Commented Jun 21, 2012 at 1:36
  • We're using OneToOneField in our djangoappengine project so I think it works and the blog post is out of date. Still, you have taken the logical next step which is to denormalise the child data onto the parent model and avoid the extra query to access them... this is particularly important on GAE where the datastore is very slow. On Mongo you'll have the advantage you can also query into the embedded model I think too? Shame GAE dict fields can't do that.
    – Anentropic
    Commented Jun 21, 2012 at 12:52
  • Official?? docs at django-nonrel.readthedocs.org/en/latest/content/… does not list OneToOneField as unsupported. Commented Jul 16, 2012 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.