我不理解数据库结构中那些’ ‘字段的含义,它们的目的是什么。

huangapple go评论97阅读模式
英文:

I dont understand what those '' field mean in a structure for database, what it is the purpose of

问题

以下是要翻译的内容:

结构体中的orm:"auto"orm:"size(100)"有什么作用?

我的意思是,我知道这些字段对应于我在数据库中设置的限制,但为什么它们出现在代码中?为什么没有像这样的结构体?

type User struct {
  Id   int    
  Name string 
}

这样做有什么不同?我不明白。谢谢你的阅读和帮助。

英文:
type User struct {
  Id   int    `orm:"auto"`
  Name string `orm:"size(100)"`
}

what the purpose of 'orm:"auto"' and 'orm:"size(100)"'.

i mean i know those field corresponds to the limits that i have set in my database, but why they are here in the code ? why there is not a structure like this ?

type User struct {
  Id   int    
  Name string 
}

does it changes something ? i dont understand. thanks for reading and helping me.

答案1

得分: 1

首先,看起来你没有使用GORM,而是使用了其他东西。我会假设你是在这里使用的。

这会有什么变化吗?

是的。这些标签为所讨论的字段添加了额外的属性。

例如,"auto"使字段自动递增,"size(100)"……嗯,如果你稍微动动脑筋,我想你应该能猜到它的作用!

为什么没有这样的结构?

有的!

下面这个替代结构是完全有效的,只是不符合你代码的作者的意图:

type Result struct {
    Name string
    Age  int
}

请参考文档以了解可以使用的标签。你需要阅读不止包描述的前几段,才能了解这项技术的工作原理!

**免责声明:**我从来没有在我的生活中使用过Go或这个库。上述内容来自快速搜索和浏览手册。

英文:

<sup>First of all, it appears that you are not using GORM but something else. I will assume that herein.</sup>

> does it changes something ?

Yes. These are tags that add extra attributes to the field in question.

For example, &quot;auto&quot; makes the field auto-increment, and &quot;size(100)&quot;&hellip; well, I think you can probably guess if you apply a little brainpower!

> why there is not a structure like this ?

There is!

The following alternative structure is perfectly valid, just not what the author of your code intended:

type Result struct {
    Name string
    Age  int
}

Consult the documentation to discover what tags you can use. You'll have to read more than just the first few paragraphs of the package description to find out how the technology works!

<sub>Disclaimer: Never used Go or this library in my life. The above comes from a quick Google and glance at the manual.</sub>

huangapple
  • 本文由 发表于 2017年1月21日 01:26:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/41769220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定