如何在Go中的AppEngine的数据存储区中建立多对多关系?

I'm trying to wrap my head around how I can represent a many-to-many relationship inside of AppEngine's Datastore in the Go Programming Language. I'm more used to traditional relational databases.

I have two types of entities in my system. Let's call them A and B. Every A entity is related to some number of B entities. Similarly, every B entity is related to some other number of A entities. I'd like to be able to efficiently query for all B entities given an A entity, and for all A entities given a Bentity.

In the Python SDK, there seems to be a way to note fields in an entity can be ReferencePropertys which reference some other entity. However, I can't find something similar in Go's AppEngine SDK. Go seems to just use basic structs to represent entities.

What's the best practice for dealing with this?

based on how you which to query you could do the following:

in your struct A add a field:

BIds []int64

in your struct B add a field:

AIds []int64

now any time you add a relation between A and B you just need to add the corresponding ids to your two variables

when you need to query now for all B which are related to this A1 you do your query like this:

SELECT * FROM B where AIds = 'A1'

for all A wich are related to this B1 your do it similar:

SELECT * FROM A where BIds = 'B1'

update:
altered querys on suggestion from dragonx

A python ReferenceProperty essentially stores a key to another entity. It's similar to using a Key field in Go.

There's at least two ways to solve your problem. A cheap way to store a limited number of references, and an expensive way for larger data sets.

fmt.Println.MKO provided the answer for the cheap way, except the query is simpler than what he suggests, it should actually be:

SELECT * FROM B where AIds = 'A1'

This method is limited to the number of indexed entries per entity, as well as the entity size. So the list of AIds or BIds will limit the number of entities to 20000 or less.

If you have an insane amount of data, you would want a mapping entity to represent the M2M relationship between a given A & B entity. It would simply contain a key to an A and a key to a B. You would then query for map entities, and then fetch the corresponding A or B entities you need. This would be much more expensive, but breaks past the entity size limit.