普通网友 2014-09-25 10:16
浏览 26
已采纳

Google App Engine:Go中的一对多关系

I am currently in the process on deciding which stack I'll use for a (small) social network . I would love to code it in golang on top of Google App Engine since this would result in clean, fast code with all the advantages of modern cloud services & hosting.

How would you implement one-to-many and/or many-to-one relationships in GAE with golang? According to the docs you can use your own types in datastore-structs, as long as their properties are the ones mentioned in the documentation. So for example:

type Comment struct {
    Title    string  
    Text     string
    User     *User 
}

type Photo struct {
    Description   string
    Comments      []*Comment
}

should work in my opinion, am I right? And if I am right, how can I:

a) populate this relationship in one query?

b) add comments to a photo in a convenient way? (such as MongoDBs '$push')

best, Michel

  • 写回答

1条回答 默认 最新

  • dpno17028 2014-09-25 16:46
    关注

    On App Engine, you can model this with two entity types:

    type Comment struct {
        Title    string  
        Text     string
        User     *User 
    }
    
    type Photo struct {
      Description   string
    }
    

    Use an ancestor path to associate child comments with a parent photo. You can get all comments for a photo in a single query. You can add comments by putting new comment entities.

    I think you can store a photo using the types in the question (it looks like you can according to the doc, but I have no personal experience with nesting three levels in an entity). To add a comment with this design, the application fetches the photo (this will include the comments), adds the comment to the photo, and puts the photo with all comments back to the datastore.

    App Engine does not have entity modification operators like Mongo's $push.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度