donglou1866 2013-06-28 18:22
浏览 16

如何在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?

  • 写回答

2条回答 默认 最新

  • doumeng9188 2013-06-28 19:15
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入