Hello, I'm trying to change the name of the default columns createdAt, updatedAt and deletedAt to another language, in this case pt-BR. My code in Struct Table:
type Company struct {
gorm.Model
ID uuid.UUID `json:"id" `
Cnpj string `json:"cnpj" gorm:"column:cnpj"`
CorporateName string `json:"corporate_name" gorm:"column:razao_social"`
CompanyName string `json:"company_name" gorm:"column:nome_fantasia"`
Address string `json:"address" gorm:"column:endereco"`
ZipCode string `json:"zip_code" gorm:"column:cep"`
CityName string `json:"city_name" gorm:"column:cidade"`
AddressNumber int `json:"address_number" gorm:"column:numero"`
Neighborhood string `json:"neighborhood" gorm:"column:bairro"`
CreatedAt time.Time `json:"createdAt" gorm:"column:criado_em"`
UpdatedAt time.Time `json:"updatedAt" gorm:"column:editado_em"`
DeletedAt time.Time `json:"deletedAt" gorm:"column:deletado_em"`
}
result when I run the insert query:
INSERT INTO "empresas" ("created_at","updated_at","deleted_at","cnpj","razao_social","nome_fantasia","endereco","cep","cidade","numero","bairro","criado_em","editado_em","deletado_em") VALUES ('2022-03-11 09:18:24.738','2022-03-11 09:18:24.738',NULL,'12345678912345','Wagner w&w','Wagner Ricardo aps','Rua das cerejeiras','98910000','Três de Maio',456,'Loteamento Sartori','2022-03-11 09:18:24.738','2022-03-11 09:18:24.738','0000-00-00 00:00:00') RETURNING "id","id"
2022/03/11 09:18:24 db error ERROR: column "created_at" of relation "empresas" does not exist (SQLSTATE 42703)
Note that the gorm identifies the custom fields, but the originals are still embedded in the queries.
How do I just leave the custom fields?
https://gorm.io/docs/conventions.html#Column-Name
Expected answer
INSERT INTO "empresas" ("criado_em","editado_em","deletado_em","cnpj","razao_social","nome_fantasia","endereco","cep","cidade","numero","bairro") VALUES ('2022-03-11 09:18:24.738','2022-03-11 09:18:24.738',NULL,'12345678912345','Wagner w&w','Wagner Ricardo aps','Rua das cerejeiras','98910000','Três de Maio',456,'Loteamento Sartori') RETURNING "id","id"
Comment From: a631807682
type Company struct {
gorm.Model
...
}
gorm.Model already defined ID/CreatedAt/UpdatedAt/DeletedAt, remove it and try again.
https://github.com/go-gorm/gorm/blob/master/model.go#L10
Comment From: 260721735
you can do like this
type OfficialModel struct {
gorm.Model
}
type MyModel struct {
GormModel
}
type GormModel struct {
ID uint `gorm:"primarykey" json:"id" form:"id"`
CreatedAt time.Time `gorm:"column:criado_em;"json:"criado_em" form:"criado_em"`
UpdatedAt time.Time `gorm:"column:editado_em;"json:"editado_em" form:"editado_em"`
DeletedAt gorm.DeletedAt `gorm:"column:deletado_em;"gorm:"index" json:"deletado_em" form:"deletado_em"`
}
func main() {
mysql := db.GetDB()
model1 := db.OfficialModel{}
model2 := db.MyModel{}
mysql.Create(&model1)
mysql.Create(&model2)
mysql.Updates(&model1)
mysql.Updates(&model2)
mysql.Delete(&model1)
mysql.Delete(&model2)
}
you can get this result
but the limit is you must use CreatedAt,UpdatedAt,DeletedAt in your GormModel because gorm use this name in code
https://github.com/go-gorm/gorm/blob/0097b39a77b9573d63f89c22f3cea0aae103a77f/schema/field.go#L277
Comment From: wagnerww
@260721735 thanks! I found it confusing and different from other orm patterns like TypeOrm and Hibernate. But, I believe that in order not to have problems in the future, I will have to keep the default gorm names in my database: created_at, update_at and deleted_at
Hope this will be seen in the near future