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

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

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,你可以将其嵌入到你的模型中,或者只写入你想要的字段”:

// 基本模型的定义
type Model struct {
  ID        uint `gorm:"primary_key"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt *time.Time
}

// 添加字段 `ID`、`CreatedAt`、`UpdatedAt`、`DeletedAt`
type User struct {
  gorm.Model
  Name string
}

// 只需要字段 `ID`、`CreatedAt`
type User struct {
  ID        uint
  CreatedAt time.Time
  Name      string
}

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

type User struct {
  gorm.Model
  ID        uint
  CreatedAt time.Time
  Name      string
}

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

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

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

type CreateUser struct {
  gorm.Model
  Name string
}

type RetrieveUser struct {
  gorm.Model
  ID        uint
  CreatedAt time.Time
  Name      string
}

这样我就可以执行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":

// Base Model's definition
type Model struct {
  ID        uint `gorm:"primary_key"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt *time.Time
}

// Add fields `ID`, `CreatedAt`, `UpdatedAt`, `DeletedAt`
type User struct {
  gorm.Model
  Name string
}

// Only need field `ID`, `CreatedAt`
type User struct {
  ID        uint
  CreatedAt time.Time
  Name      string
}

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

type User struct {
  gorm.Model
  ID        uint
  CreatedAt time.Time
  Name      string
}

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

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

So I have to have two different objects :

type CreateUser struct {
  gorm.Model
  Name string
}

type RetrieveUser struct {
  gorm.Model
  ID        uint
  CreatedAt time.Time
  Name      string
}

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

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

答案1

得分: 3

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

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

type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}

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

英文:

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

type Model struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
}

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:

确定