【问题标题】:Modeling N to N associations in Go on the App Engine在 App Engine 上的 Go 中对 N 到 N 关联建模
【发布时间】:2011-12-21 20:41:25
【问题描述】:

我正在尝试使用 Google App Engine 在 Go 中编写一个网络应用程序,但我有一个关于使用数据存储区建模关系的问题。

我知道在 Python 中我可以使用 db.referenceProperty() 对关系进行建模。我似乎无法弄清楚如何使用 Go API 进行类似的关联。

有没有人遇到过这种情况?

【问题讨论】:

    标签: google-app-engine go


    【解决方案1】:

    您可以在实体中使用 Key 作为属性:http://code.google.com/appengine/docs/go/datastore/reference.html

    类似这样的东西(我不知道 Go 所以请耐心等待):

    type Employee struct {
        Name     string
        Boss     *Key
    }
    
    employee := Employee{
        Name:     "John Doe",
        Boss:     key    // a key to some other entity
    }
    

    【讨论】:

      【解决方案2】:

      彼得,你绝对是在正确的轨道上。我认为我已经想通了。我还没有真正测试过这个,但它似乎在数据存储查看器中是正确的。我现在拥有的看起来像这样(忽略示例的错误检查):

      type Boss struct {
          Name, Uuid string
      }
      
      type Employee struct {
          Name, Uuid string,
          Boss *datastore.Key
      }
      
      boss := &Boss {
          Name:  "Pointy Haired Boss",
          Uuid:  <<some uuid>>,
      }
      
      dilbert := &Employee {
          Name: "Dilbert",
          Uuid: <<some uuid>>,
          boss: nil,
      }
      
      datastore.Put(context, datastore.NewIncompleteKey(context, "Boss", nil), bossman)
      query := datastore.NewQuery("Boss").Filter("Uuid =", bossMan)
      for t := query.Run(ctx); ; {
          var employee Employee
          key, err := t.Next(&employee)
          if err == datastore.Done {
              break
          }
          if err != nil {
              fmt.Fprintf(w, "Error %s", err)
          }
          dilbert.Boss = key
      }
      datastore.Put(context, datastore.NewIncompleteKey(context, "Employee", nil), dilbert)
      

      【讨论】:

        猜你喜欢
        • 2014-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-17
        • 1970-01-01
        • 2010-10-16
        • 2015-03-27
        相关资源
        最近更新 更多