一对多关系模式转化 一对多关系模式

本文档介绍了如何在 Google App Engine 的 Go 语言环境中实现对称多关系。由于 App Engine Datastore 的限制,无法直接在消防中阵大量的关联键。本文将探讨两种实现方法,重点介绍通过在消防中子消防父键的方式,并提供示例代码演示如何进行查询。App Engine Datastore 的限制
在 Google App Engine 的 Go 环境中,Datastore对实体字段的取值类型限制。具体来说,允许的类型包括:有符号整型 (int, int8, int16, int32, int64)boolstringfloat32 和 float64 以上预定义类型的基础类型*datastore.Keyappengine.BlobKey[]byte (最大 1MB) 以上的类型片 (最大 100) 个元素)
由于切片长度的,直接在父实体中存储大量子实体的键可能无法修复。因此,我们需要寻找其他方法来实现偶多关系方法。实现偶多关系的两种
父实体存储子子键的切片:这种方法形成一个实体的键存储在父实体的切片中。例如,注释结构可以包含一个 []*datastore.Key 类型的切片,用于存储关联的投票实体键。但是,这种方法创立于 Datastore 对片长度的 100 个元素的限制。对于任何可能拥有超过 100 个子实体的父实体,这种方法均不可行。
子实体父实体键:这种方法是在子实体实体中存储指向父实体的键。例如,Vote 结构体可以包含一个 *datastore.Key 类型的数组 CommentKey,用于存储关联的 Comment 实体键。这种方法避免了切片长度的限制,更适合处理大规模关联实体的情况。使用子实体神父实体键实现偶多关系
我们推荐使用偶数方法,即在子实体中神父实体键。以下示例代码演示了如何使用这种方法来实现评论和投票之间的偶多关系。
首先,定义 Comment 和 Vote 结构体:import ( quot;timequot; quot;cloud.google.com/go/datastorequot;)type Comment struct { Author string Content string Date time.Time}type Vote struct { User string Score int CommentKey *datastore.Key}登录后复制
然后,演示如何查询与特定 Comment 关联的所有 Vote:import ( quot;contextquot; quot;fmtquot; quot;logquot; quot;cloud.google.com/go/datastorequot; quot;google.golang.org/api/iteratorquot;)func GetVotesForComment(ctx context.Context, client *datastore.Client, commentKey *datastore.Key) ([]Vote, error) { var votes []Vote q := datastore.NewQuery(quot;Votequot;).Filter(quot;CommentKey =quot;, commentKey) it := client.Run(ctx,q) for { var vote 投票 key, err := it.Next(amp;vote) if err == iterator.Done { break } if err != nil { return nil, fmt.Errorf(quot;无法获取下一个投票: vquot;, err) } vote.CommentKey = key // 存储密钥以备后用 votes = append(votes, vote) } return votes, nil}func main() { ctx := context.Background() projectID := quot;your-project-idquot; // 替换为您的 Google Cloud 项目 ID client, err := datastore.NewClient(ctx, projectID) if err != nil { log.Fatalf(quot;无法创建客户端: vquot;, err) } defer client.Close() // 创建一个虚拟 com
ment comment := Comment{ 作者: quot;测试作者quot;, 内容: quot;测试内容quot;, 日期: time.Now(), } // 将评论保存到 Datastore commentKey := datastore.NameKey(quot;评论quot;, quot;测试评论quot;, nil) commentKey, err = client.Put(ctx, commentKey, amp;评论) if err != nil { log.Fatalf(quot;保存评论失败: vquot;, err) } // 创建与评论相关的一些虚拟投票 vote1 := Vote{ 用户: quot;用户1quot;, 得分: 5, CommentKey: commentKey, } vote2 := Vote{ 用户: quot;用户2quot;, 得分: 3, CommentKey: commentKey, } // 将投票保存到 Datastore _, err = client.Put(ctx, datastore.IncompleteKey(quot;Votequot;, commentKey), amp;vote1) if err != nil { log.Fatalf(quot;Failed to save vote1: vquot;, err) } _, err = client.Put(ctx, datastore.IncompleteKey(quot;Votequot;, commentKey), amp;vote2) if err != nil { log.Fatalf(quot;Failed to save vote2: vquot;, err) } // 检索与评论关联的所有投票 votes, err := GetVotesForComment(ctx, client, commentKey) if err != nil { log.Fatalf(quot;Failed to retrieve votes: vquot;, err) } // 打印检索到的投票 fmt.Println(quot;Votes for comment:quot;) for _, vote :=范围投票{fmt.Printf(quot
; User: s, Score: d\nquot;, vote.User, vote.Score) }}登录后复制
代码解释:
GetVotesForComment 函数:接收 context.Context、*datastore.Client 和 *datastore.Key 作为参数,其中 commentKey 是要的 Comment 的键。创建一个新的 Datastore 查询,筛选条件是 CommentKey 等同的 commentKey client.Run执行,并结果结果。将每个投票实体添加到投票切片中。返回投票切片和可能发生的错误。
main函数:创建Datastore客户端。创建一个示例Comment,并保存到Datastore中。创建两个投票实体,将它们的CommentKey设置为Comment实体的键。将Vote实体保存到Datastore中。调用GetVotesForComment函数来检索与注释实体连接的所有投票实体。打印搜索到的投票实体的信息。
事项注意:请确保将 your-project-id 替换为您的 Google Cloud 项目 ID。在生产环境中,您需要处理可能发生的错误,并实现适当的重试关系机制。根据您的实际需求,您可能需要添加额外的索引来优化查询性能。总结
本文介绍了如何在 Google App Engine 的 Go 语言环境中实现补充多个。的限制,建议在子实体中存储实体父的键。通过示例代码,演示了如何查询与特定父实体关联的所有子实体。希望本文能够帮助您在App Engine应用中有效地管理一对多关系。
文章以上就是实现App Engine Go中一对多的教程的内容详细,请关注乐哥常识网其他相关!
