Google Datastore在Python中是一个很好的模型

时间:2014-08-26 09:42:23

标签: python google-app-engine google-cloud-datastore nosql

您好我是Datastore和Python的新手,但我必须对我的数据建模并将其摄取到Google数据存储区,我有点迷失了!

我阅读了有关使用Python建模实体关系和数据建模的所有内容(因此我理解了一对多或多对多的关系)。但我有一个简单的问题,我不知道如何回答!

例如,我的数据是运动匹配的结果。在基本的SQL数据库中,我会有类似的东西:

表:匹配| IDmatch 比赛名称
比赛日 优胜者 IDteam1 IDteam2

然后

表:TEAM | IDteam 选手姓名 playerbirth playerposition

所以我能够简单地知道所有参与比赛的球员的名字。它在数据库中如何工作? Doi我必须使用一对多或多对多的关系或其他什么?

谢谢你的帮助:)

1 个答案:

答案 0 :(得分:3)

对于Match,Team和Player,你会create entities。您可以选择让团队成为玩家的parent entity,但是如果玩家移动团队,您可以选择通过Key Properties将两者联系起来。您可以选择将团队数据去规范化到匹配中以便于检索(因为您无法执行JOIN查询)。

典型的模型可能如下所示:

class Team(ndb.Model):
  name = ndb.StringProperty()

class Player(ndb.Model):
  name = ndb.StringProperty()
  date_of_birth = ndb.DateProperty()
  position = ndb.StringProperty()
  team = ndb.KeyProperty(kind=Team)

  # denormalized properties
  team_name = ndb.StringProperty()

class Match(ndb.Model):
  name = ndb.StringProperty()
  team1 = ndb.KeyProperty(kind=Team)
  team2 = ndb.KeyProperty(kind=Team)
  winning_team = ndb.KeyProperty(kind=Team)

  # denormalized properties
  team1_name = ndb.StringProperty()
  team2_name = ndb.StringProperty()
  winning_team_name = ndb.StringProperty()

这应该是让你前进的起点。