de-normalizing data model: django/sql -> app engine

与世无争的帅哥 提交于 2020-01-07 02:21:29

问题


I'm just starting to get my head around non-relational databases, so I'd like to ask some help with converting these traditional SQL/django models into Google App Engine model(s).

The example is for event listings, where each event has a category, belongs to a venue, and a venue has a number of photos attached to it.

In django, I would model the data like this:

class Event(models.Model)
    title = models.CharField()
    start = models.DatetimeField()
    category = models.ForeignKey(Category)
    venue = models.ForeignKey(Venue)

class Category(models.Model):
    name= models.CharField()

class Venue (models.Model):
    name = models.CharField()
    address = models.CharField()

class Photo(models.Model):
    venue = models.ForeignKey(Venue)
    source = models.CharField()

How would I accomplish the equivalent with App Engine models?


回答1:


There's nothing here that must be de-normalized to work with App Engine. You can change ForeignKey to ReferenceProperty, CharField to StringProperty and DatetimeField to DateTimeProperty and be done. It might be more efficient to store category as a string rather than a reference, but this depends on usage context.

Denormalization becomes important when you start designing queries. Unlike traditional SQL, you can't write ad-hoc queries that have access to every row of every table. Anything you want to query for must be satisfied by an index. If you're running queries today that depend on table scans and complex joins, you'll have to make sure that the query parameters are indexed at write-time instead of calculating them on the fly.

As an example, if you wanted to do a case-insensitive search by event title, you'd have to store a lower-case copy of the title on every entity at write time. Without guessing your query requirements, I can't really offer more specific advice.




回答2:


It's possible to run Django on App Engine

You need a trio of apps from here: http://www.allbuttonspressed.com/projects

  • Django-nonrel
  • djangoappengine
  • djangotoolbox

Additionally, this module makes it possible to do the joins across Foreign Key relationships which are not directly supported by datastore methods:

  • django-dbindexer

...it denormalises the fields you want to join against, but has some limitations - doesn't update the denormalised values automatically so is only really suitable for static values

Django signals provide a useful starting point for automatic denormalisation.



来源:https://stackoverflow.com/questions/8066941/de-normalizing-data-model-django-sql-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!