Description

The following is detail SQLs, we can see Updates will create an empty record if object_stores record has already been marked as deleted it references, and it will update the reference object_store_id to 5 (new created, it should be 4).

2022/06/08 16:33:56 /root/go/src/deeproute.ai/smd/pkg/models/object_storage_class.go:77
[1.068ms] [rows:1] INSERT INTO "object_stores" ("created_at","updated_at","deleted_at","name","status","preserve_pools_on_
delete") VALUES ('2022-06-08 16:33:56.411','2022-06-08 16:33:56.411',NULL,'','',true) ON CONFLICT DO NOTHING RETURNING "id
"

2022/06/08 16:33:56 /root/go/src/deeproute.ai/smd/pkg/models/object_storage_class.go:77
[4.460ms] [rows:1] UPDATE "object_storage_classes" SET "object_store_id"=5,"status"='deleting',"updated_at"='2022-06-08 16
:33:56.412' WHERE "object_storage_classes"."deleted_at" IS NULL AND "id" = 3
 72 // NOTE: if fields not specified, UpdateXXX attributes with `struct`, will only update non-zero fields
 73 func (m *Model) UpdateObjectStorageClass(in *ObjectStorageClass, fields ...map[string]interface{}) (err error) {
 74     if len(fields) == 0 {                                                                           
 75         err = m.DB().Updates(in).Error                                                              
 76     } else {                                                                                        
 77         err = m.DB().Model(in).Updates(fields[0]).Error                                                               
 78     }                                                                                               
 79     if err != nil {                                                                                 
 80         return err                                                                                  
 81     }                                                                                               
 82                                                                                                     
 83     return nil                                                                                      
 84 } 
type ObjectStorageClass struct {                                                                        
    gorm.Model                                                                                          

    Name   string `gorm:"unique"`                                                                       
    Status string                                                                                       

    ObjectStoreID uint                                                                                  
    ObjectStore   *ObjectStore 
}

type ObjectStore struct {                                                                                                 
    gorm.Model                                                                                      

    Name   string `gorm:"unique"`                                                                   
    Status string                                                                                   

    PreservePoolsOnDelete *bool `gorm:"default:true"`
}

The records in database table

smd=# select * from object_stores;
 id |          created_at           |          updated_at           |          deleted_at          |  name   |  status  | preserve_pools_on_delete 
----+-------------------------------+-------------------------------+------------------------------+---------+----------+--------------------------
  4 | 2022-06-08 08:29:53.437299+00 | 2022-06-08 08:32:37.150391+00 | 2022-06-08 08:32:46.29671+00 | os-test | Deleting | f
  5 | 2022-06-08 08:33:56.41143+00  | 2022-06-08 08:33:56.41143+00  |                              |         |          | t
(2 rows)

smd=# select * from object_storage_classes;
 id |          created_at           |          updated_at           |          deleted_at           |  name  |  status  | object_store_id 
----+-------------------------------+-------------------------------+-------------------------------+--------+----------+-----------------
  5 | 2022-06-08 08:32:02.530823+00 | 2022-06-08 08:32:02.540314+00 |                               | os-sc  | active   |               4
  3 | 2022-06-08 08:31:57.915888+00 | 2022-06-08 08:33:56.412563+00 | 2022-06-08 08:33:56.425147+00 | os-sc2 | deleting |               5
(2 rows)

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: microyahoo

@jinzhu

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: microyahoo

无语