The below is golang struct for the table where settings will be stored recursively.

// Settings table in database
type Settings struct {
    ID          uint       `json:"id"`
    Name        string     `json:"name"`
    Value       string     `json:"value"`
    Parent_ID   uint       `json:"parent_id" gorm:"default:null"`
    Description string     `json:"description"`
    Parent      *Settings  `gorm:"foreignKey:Parent_ID"`
    Children    []Settings `gorm:"foreignKey:Parent_ID" json:"Children"`
}

When Passing the below json to db.Save(&settings) to update the settings table which already has parent record with ID 30 and its child record with ID 31, on duplicate key updates works fine for parent record.

[
    {
        "id": 30,
        "name": "seal",
        "value": "XXXX.png",
        "parent_id": 0,
        "description": "",
        "Parent": null,
        "Children": [
            {
                "id": 31,
                "name": "sealcontent",
                "value": "test data dddd",
                "parent_id": 0,
                "description": "Child Description 2-1",
                "Parent": null,
                "Children": null
            }
        ]
    }
]

The issue is for child record as it only updates parent_id for the child row. Corresponding sql query generated by gorm is given below.

INSERT INTO settings (name,value,description,parent_id,id) VALUES ('sealcontent','test data dddd','Child Description 2-1',30,31) ON DUPLICATE KEY UPDATE parent_id=VALUES(parent_id)

Please help me how to achieve the below query for child record updates on duplicate key case so that new vales for all the fields can be updated.

INSERT INTO settings (name,value,description,parent_id,id) VALUES ('sealcontent','test data dddd','Child Description 2-1',30,31) ON DUPLICATE KEY UPDATE parent_id=name=VALUES(name),value=VALUES(value),description=VALUES(description),VALUES(parent_id)

The document you expected this should be explained

Expected answer

In Summary

What I am getting now for child record update INSERT INTO settings (name,value,description,parent_id,id) VALUES ('sealcontent','test data dddd','Child Description 2-1',30,31) ON DUPLICATE KEY UPDATE parent_id=VALUES(parent_id)

How I need

INSERT INTO settings (name,value,description,parent_id,id) VALUES ('sealcontent','test data dddd','Child Description 2-1',30,31) ON DUPLICATE KEY UPDATE parent_id=name=VALUES(name),value=VALUES(value),description=VALUES(description),VALUES(parent_id

Comment From: fronge

I have encountered the same problem. How was it resolved?

Comment From: fronge

I use db.Session(&gorm.Session{FullSaveAssociations: true }).Updates(&user)