gpt4 book ai didi

google-app-engine - golang 数据存储结构 : keeping field unique and required

转载 作者:数据小太阳 更新时间:2023-10-29 03:27:53 27 4
gpt4 key购买 nike

我想知道如何最好地保证一个字段是唯一的,如果不是,则不会保存到数据存储中。另外,它应该是必需的。我将此字段用作 stringID 并需要它是唯一的。我知道我可以简单地尝试通过该字段获取实体并查看它是否存在并围绕它构建逻辑。但是有没有更简单的方法,比如在您的结构中声明该字段应该是唯一的和/或必需的?就像下面的模型。

type Car struct {
Regnr string "required" "unique"
}

谢谢!

最佳答案

来自数据存储 API:

By default, for struct pointers, all properties are potentially indexed, and the property name is the same as the field name (and hence must start with an upper case letter). Fields may have a datastore:"name,options" tag. The tag name is the property name, which must be one or more valid Go identifiers joined by ".", but may start with a lower case letter. An empty tag name means to just use the field name. A "-" tag name means that the datastore will ignore that field. If options is "noindex" then the field will not be indexed. If the options is "" then the comma may be omitted. There are no other recognized options.

无法使用 Datastore 设置这些类型的标签。

关于google-app-engine - golang 数据存储结构 : keeping field unique and required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34575542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com