douchao0358 2013-10-16 05:16
浏览 44

如何使用Go的dataStore创建数据模型?

I've created a series of structs based on the way I would create tables in mySQL:

type User struct {
    UserID      int
    Email       string
    Password    string
    DateCreated time.Time
}

type Device struct {
    DeviceID      int
    Udid          string
    DateCreated   time.Time
    DateUpdated   time.Time
    IntLoginTotal int
}

type DeviceInfo struct {
    DeviceID       int
    DeviceName     string
    Model          string
    LocalizedModel string
    SystemName     string
    SystemVersion  string
    Locale         string
    Language       string
    DateCreated    time.Time
}

However, I have the impression that I will not be able to make requests like this, and instead I need to create a single struct that can contain an array of multiple devices (each containing an array of multiple device info records).

What is the best way to go about doing this?

  • 写回答

1条回答 默认 最新

  • drvonr6573 2013-10-16 10:26
    关注

    In this case, just set the "Kind" as the name of the struct.

    From the docs:

    func NewKey(c appengine.Context, kind, stringID string, intID int64, parent *Key) *Key
    

    NewKey creates a new key. kind cannot be empty. Either one or both of stringID and intID must be zero. If both are zero, the key returned is incomplete. parent must either be a complete key or nil.

    For example to save a User.

    c := appengine.NewContext(r)
    u := &User{UserID: userid, Email: email, Password: password, DateCreated: datecreated}
    k := datastore.NewKey(c, "User", u.UserID, 0, nil)
    e := p
    _, err := datastore.Put(c, k, e)
    

    Follow the same logic to save a different struct type.

    To load a user:

    c := appengine.NewContext(r)
    
    k := datastore.NewKey(c, "User", userid, 0, nil)
    e := new(User)
    
    _, err := datastore.Get(c, k, e)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端