type Contact struct {
gorm.Model
PersonID sql.NullInt64
}
type Person struct {
gorm.Model
}
I am trying to use gorm with mysql in the previuos code but I have the following problem:
I want:
sql.NullInt64
to work easily with null values. gorm.Model
, including fields ID
, CreatedAt
, UpdatedAt
, DeletedAt
. Db.Model(&models.Contact{}).AddForeignKey
. My problem:
Person.ID
become "int(10)"
in mysql. Contact.PersonID
become "bigint(20)"
Some body can help me to solve this?
The "magic" on gorm.Model
is only the name of the fields, any struct
with these fields look like this according to the gorm documentation, at the end of Conventions
For example: Save records having UpdatedAt
field will set it to current time.
Or
Delete records having DeletedAt
field, it won't be deleted from database, but only set field DeletedAt
's value to current time, and the record is not findable when querying, refer Soft Delete
So solve the issue is very easy, this is the code for my case:
package models
import "time"
type Model struct {
ID uint `gorm:"primary_key;type:bigint(20) not null auto_increment"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
So, now I only need use it as base model :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.