在使用gorm(golang)读取和写入数据库时,我是否需要有两个不同的对象?

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

Do I need to have two different objects to read and to write in my database using gorm (golang)?

问题

gorm在文档中提到:“基本模型定义gorm.Model,包括字段ID、CreatedAt、UpdatedAt、DeletedAt,你可以将其嵌入到你的模型中,或者只写入你想要的字段”:

  1. // 基本模型的定义
  2. type Model struct {
  3. ID uint `gorm:"primary_key"`
  4. CreatedAt time.Time
  5. UpdatedAt time.Time
  6. DeletedAt *time.Time
  7. }
  8. // 添加字段 `ID`、`CreatedAt`、`UpdatedAt`、`DeletedAt`
  9. type User struct {
  10. gorm.Model
  11. Name string
  12. }
  13. // 只需要字段 `ID`、`CreatedAt`
  14. type User struct {
  15. ID uint
  16. CreatedAt time.Time
  17. Name string
  18. }

根据文档的说明,我期望只有一个User的定义,所以我创建了一个如下的对象:

  1. type User struct {
  2. gorm.Model
  3. ID uint
  4. CreatedAt time.Time
  5. Name string
  6. }

但是,如果我执行DB.CreateTable(&User{}),我会从PostgreSQL得到以下错误:

  1. (pq: column "id" specified more than once)
  2. (pq: column "created_at" specified more than once)

所以我必须有两个不同的对象:

  1. type CreateUser struct {
  2. gorm.Model
  3. Name string
  4. }
  5. type RetrieveUser struct {
  6. gorm.Model
  7. ID uint
  8. CreatedAt time.Time
  9. Name string
  10. }

这样我就可以执行DB.CreateTable(&CreateUser{})

这样做很丑陋,我肯定是漏掉了什么,有什么想法吗?

英文:

gorm tell in the documentation that "Base model definition gorm.Model, including fields ID, CreatedAt, UpdatedAt, DeletedAt, you could embed it in your model, or only write those fields you want":

  1. // Base Model's definition
  2. type Model struct {
  3. ID uint `gorm:"primary_key"`
  4. CreatedAt time.Time
  5. UpdatedAt time.Time
  6. DeletedAt *time.Time
  7. }
  8. // Add fields `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`
  9. type User struct {
  10. gorm.Model
  11. Name string
  12. }
  13. // Only need field `ID`, `CreatedAt`
  14. type User struct {
  15. ID uint
  16. CreatedAt time.Time
  17. Name string
  18. }

Following the documentation, I expect to have only one definition of User, so I create an object like that:

  1. type User struct {
  2. gorm.Model
  3. ID uint
  4. CreatedAt time.Time
  5. Name string
  6. }

But if I do a DB.CreateTable(&User{}), I get the following errors from postgres:

  1. (pq: column "id" specified more than once)
  2. (pq: column "created_at" specified more than once)

So I have to have two different objects :

  1. type CreateUser struct {
  2. gorm.Model
  3. Name string
  4. }
  5. type RetrieveUser struct {
  6. gorm.Model
  7. ID uint
  8. CreatedAt time.Time
  9. Name string
  10. }

So I can do a DB.CreateTable(&CreateUser{})

It is very ugly and I must be missing something, any idea?

答案1

得分: 3

好的,以下是翻译好的内容:

好的,我刚刚阅读了gorm.Model背后的代码,我得到了答案。

  1. type Model struct {
  2. ID uint `gorm:"primary_key"`
  3. CreatedAt time.Time
  4. UpdatedAt time.Time
  5. DeletedAt *time.Time `sql:"index"`
  6. }

这意味着我刚刚学会了在Go语言中如何使用继承!

英文:

Ok, just read the code behind gorm.Model and I got my answer.

  1. type Model struct {
  2. ID uint `gorm:"primary_key"`
  3. CreatedAt time.Time
  4. UpdatedAt time.Time
  5. DeletedAt *time.Time `sql:"index"`
  6. }

It means I just learned how inheritance works in go !

huangapple
  • 本文由 发表于 2016年12月10日 05:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/41069390.html
匿名

发表评论

匿名网友

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

确定