GORM Playground Link

For exam

https://github.com/go-gorm/playground/pull/1

Description

What is happening?

I have a Quiz model which has a list Questions of questions as an association and when I get a Quiz by preloading all its Questions and then save the Quiz. It creates new question records ( = number of questions already existing).

       r := database.DBInstance.Preload("Questions").Where("id = ? ", quizID).First(&quiz)
    if r.Error != nil {
        ctx.JSON(http.StatusInternalServerError, gin.H{"error": r.Error.Error()})
        return
    }


        quiz.Published = true
    r = database.DBInstance.Save(&quiz)
    if r.Error != nil {
        ctx.JSON(http.StatusInternalServerError, gin.H{"error": r.Error.Error()})
        return

I expect this to not create new rows for Questions when I save the Quiz.

Comment From: a631807682

If you want to update associations’s data, you should use the FullSaveAssociations mode:

https://gorm.io/docs/associations.html#Auto-Create-x2F-Update

more usage https://gorm.io/docs/associations.html#Association-Mode