简体   繁体   中英

Datastore App Engine quering ancestor python

Hello I'm new on Datastore and on Python and I have a basic question but it can help me to understand more the Google cloud.

I have 4 entities and let's say I have a parent (match) with children : team, player and event.

class Team(ndb.Model):
    d_name = ndb.StringProperty()
    d_side = ndb.StringProperty()

class Player(ndb.Model):
    d_name = ndb.StringProperty()
    date_of_birth = ndb.StringProperty()
    d_position = ndb.StringProperty()
    d_teamKey = ndb.StringProperty()


class Match(ndb.Model):
    d_competition_name = ndb.StringProperty()
    d_date = ndb.StringProperty()
    d_pool = ndb.StringProperty()
    d_season = ndb.StringProperty()
    d_team1Key = ndb.StringProperty()
    d_team2Key = ndb.StringProperty()
    d_winning_teamKey = ndb.StringProperty()
    d_match_id = ndb.StringProperty()
    d_match_day = ndb.IntegerProperty()

class Event(ndb.Expando):
    d_teamKey = ndb.StringProperty()
    d_playerKey = ndb.StringProperty()

I know that the query if I want all the matchs day 4 is :

q = ndb.gql("SELECT * FROM Match WHERE d_match_day = 4")

But how can I seach all the players in theses match's children so that I have all the players who have played during the day 4 ?

Thank you !

Add another property to Match : A StructuredProperty , which is a list of Players (and/or Teams):

Players = ndb.StructuredProperty(Player)
Teams =   ndb.StructuredProperty(Team)

Then, you can query for 4 and pull the list of Players and/or Teams.

wait.... you add the player as a child to EACH match he plays? that seems unefficient design. A child can only have 1 parent (unless I grossly misunderstood ancestor queries, which is possible, to be fair).

Anyway in one single query I don't think that's doable. I would start getting the keys from match where d_match_day = 4, and from there do a "select * from Players where match_key = " and use the list you just created. (you might need to change match_key to match your actual ancestor key, but the jist of it is there)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM