Python GAE:从RDBMS到NDB问题
我正在学习在GAE工作。我已经阅读了很多论文,所有来自谷歌的NDB文档以及这里的一些问题。我非常习惯SQL,但将过去20年的思维方式转变为NoSQL对我来说有点困难,这里给出的所有不同解决方案都让我发疯 我有下一个简单的结构: 书不能有章节 可以投票的章节 例如,《哨兵》一书可以有3章,每章分别有0票、8票和12票 在传统的SQL中,我只是从投票到章节和书籍,从章节到书籍制作外键 我这样做是为了我的模型:Python GAE:从RDBMS到NDB问题,python,google-app-engine,app-engine-ndb,Python,Google App Engine,App Engine Ndb,我正在学习在GAE工作。我已经阅读了很多论文,所有来自谷歌的NDB文档以及这里的一些问题。我非常习惯SQL,但将过去20年的思维方式转变为NoSQL对我来说有点困难,这里给出的所有不同解决方案都让我发疯 我有下一个简单的结构: 书不能有章节 可以投票的章节 例如,《哨兵》一书可以有3章,每章分别有0票、8票和12票 在传统的SQL中,我只是从投票到章节和书籍,从章节到书籍制作外键 我这样做是为了我的模型: class Book(ndb.Model): title = ndb.String
class Book(ndb.Model):
title = ndb.StringProperty(required=True)
author = ndb.StringProperty(required=True)
created = ndb.DateTimeProperty(auto_now_add=True)
# Define a default ancestor for all the books
@staticmethod
def bookKey(group='books'):
return ndb.Key(Book, group)
# Search all
@classmethod
def getAll(cls):
q = Book.query(ancestor=cls.bookKey())
q = q.order(Book.title)
books = q.fetch(100)
return books
@classmethod
def byId(cls, id):
book = Book.get_by_id(long(id), cls.bookKey())
# Get all the Chapters for a book
def getChapters(self):
chapters = Chapter.query(ancestor=self).order(Chapter.number).fetch(100)
return chapters
class Chapter(ndb.Model):
""" All chapters that a book have """
title = ndb.StringProperty(required=True)
number = ndb.IntegerProperty(default=1)
created = ndb.DateTimeProperty(auto_now_add=True)
book = ndb.KeyProperty(kind=Book)
# Search by Book (parent)
@classmethod
def byBook(cls, book, limit=100):
chapter = book.getChapters()
return chapter
# Search by id
@classmethod
def byId(cls, id, book):
return Chapter.get_by_id(long(id), parent=book)
class Vote(ndb.Model):
""" All votes that a book-chapter have """
value = ndb.IntegerProperty(default=1)
book = ndb.KeyProperty(kind=Book)
chapter = ndb.KeyProperty(kind=Chapter)
那么我的疑问是:
提前谢谢。好的开始。GAE的数据存储有点混乱。因为它是无模式的,我发现处理实体比处理数据库表更类似于处理内存中的对象/数据结构 这里有几件事我会做得不同:
- 看起来你是在一个单一的祖先下创作你所有的书。糟糕的主意。就性能而言,这让你大吃一惊。除非您需要对一组不在当前代码中的书籍执行某些事务操作,否则这是不对的
- 从Book.getChapters()函数中,似乎您希望将一本书作为一组章节的祖先。这可能是对祖先的一种很好的利用。我看不到创建章节的代码,但请确保将适当的书籍指定为祖先
- 我只想在一本书或一章中加入投票作为属性。没有必要让它成为一种单独的类型,您需要对其发出额外的查询