Reputation: 67
Hello I'm new at Datastore and Python but I have to model my data and ingest it to the google datastore and I'm a bit lost !
I read all about Modeling Entity Relationships and Data Modeling in Python (so I understand one to many or many to many relationship). But i have a simple question I don't know how to answer !
For example, my data are result of sport match. In a basic SQL databse I would have something like that :
TABLE:Match |
IDmatch
Name of the competition
matchday
winner
IDteam1
IDteam2
and then
Table:TEAM | IDteam playername playerbirth playerposition
So I would be able to know simply the name of all player who have played in the match with a certain id. How does it work in the database ? Doi I have to use one to many or many to many relationship or something else ?
Thank you to help :)
Upvotes: 0
Views: 115
Reputation: 5424
You would create entities for Match, Team and Player. You may choose to make Team a parent entity of Player, however if Players move Teams you may opt to relate the two via Key Properties instead. You may chose to de-normalize team data onto the match for easier retrieval (since you cannot perform JOIN queries).
A typical model may look like this:
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()
That should be a starting point to get you going.
Upvotes: 3