Background We are using gorm association as we have nested hierarchy. We are using Create() for creation of records across multiple tables and Updates opeartion for updating the records.
Issue During the update we do not want to change the created_at timestamp. For top level struct/ record, this is working fine and created_at column is not updated. However, for nested records created_at timestamp is changed always on performing update operation. On update, we just want to update the updated_at column and keep created_at intact.
Structs:
struct A {
ID string `gorm:"column:id;type:char(36); not null;primary_key"`
B []*B `gorm:"foreignKey:AID;references:ID"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp; not null"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp; not null"`
}
struct B {
ID string `gorm:"column:id;type:char(36); not null;primary_key"`
AID string `gorm:"column:aid;type:char(36); not null"`
C []*C `gorm:"foreignKey:BID;references:ID"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp; not null"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp; not null"`
}
struct C {
ID string `gorm:"column:id;type:char(36); not null;primary_key"`
BID string `gorm:"column:bid;type:char(36); not null"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp; not null"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp; not null"`
}
Below is the snippet of code how we are updating the records:
txn.Session(&gorm.Session{Context: ctx, FullSaveAssociations: true}).
Updates(record)
Things tried: 1. Omit: we have tried using db.omit("B.CreatedAt, B.C.CreatedAt"). However, this is not working. 2. <-:create: This leads to failure in Unit tests with reason: there is a remaining expectation which was not matched. Essentially, nested records are not persisted.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: ankit16-19
@hemantsr you found some work around for this?