We are creating a translation app with a golang and gorm backend
This is our POSTGRES database table:
enter image description here
This is our Go Struct:
type TermLocale struct {
models.Model
JsonLdId models.ResourceId `gorm:"-" jsonld:"@id" json:"-" swaggertype:"primitive,string"`
JsonLdType types.JsonString `gorm:"-" jsonld:"@type" json:"-" swaggertype:"primitive,string"`
TermID uint `api:"default" gorm:"type:int;uniqueIndex:idx_term_locales" jsonld:"-" json:"-"`
Term *Term `api:"default,resourceId" gorm:"foreignKey:TermID;references:ID" jsonld:"term" json:"term" swaggertype:"primitive,string"`
LocaleID uint `api:"default" gorm:"type:int;uniqueIndex:idx_term_locales" jsonld:"-" json:"-"`
Locale *Locale `api:"default,resourceId" gorm:"foreignKey:LocaleID;references:ID" jsonld:"locale" json:"locale" swaggertype:"primitive,string"`
Translation string `api:"default" gorm:"type:text" jsonld:"translation" json:"translation" swaggertype:"primitive,string"`
}
As you can see we have some deleted records (with deleted_at data), but we want to have the possibility to restore and update record, for exemple if the user set again a translation for the term_id and locale_id (we mark records as deleted when the user set a translation to empty string)
Today we have this error:
`ERROR: duplicate key value violates unique constraint “idx_term_locales” (SQLSTATE 23505)
[34.272ms] [rows:0] INSERT INTO “term_locales” (“created_at”,”updated_at”,”deleted_at”,”term_id”,”locale_id”,”translation”) VALUES (‘2024-06-06 17:33:25.389′,’2024-06-06 17:33:25.389′,NULL,1366,2,’test’) RETURNING “id”`
We have try to use Unscoped to get the record and remove the deleted_at with gorm.DeletedAt{} before .Updates() update record but it’s not really working (How can I restore data that I soft deleted with gorm deletedAt)
PatAfixer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.