Your Question

I have a Class Model like so :

type Class struct {
    gorm.Model
    Name        string `gorm:"type:text"`
    Year        string `gorm:"type:text"`
    TeacherID    uint
    Teacher      Teacher `gorm:"constraint:OnUpdate:CASCADE, foreignKey:TeacherID" default:"null"`
    SchoolID    uint
    School      School `gorm:"foreignKey:SchoolID"`
}

And a student Model :

type Student struct {
    gorm.Model
    Name        string `gorm:"type:text"`
    LastName    string `gorm:"type:text"`
    BirthDate   string `gorm:"type:text"`
    Username    string `gorm:"type:text"`
    Password    string `gorm:"type:text" json:"-"`
    Email       string `gorm:"type:text"`
    ClassID     uint
    Class       Class `gorm:"foreignKey:ClassID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
    SchoolID    uint
    School      School `gorm:"foreignKey:SchoolID"`
}

A student can be assigned to a class by adding the ID of the Class to the student ClassID field. But I'd also like to be able to remove students from a class by updating the ClassID field back to null.

Is there a way to do this?

I tried all I could find but didn't find a solution. I'm probably missing something but I am really lost here.

Its really frustrating that this doesn't work outright.

func RemoveStudentFromClass(c *gin.Context) {
    id := c.Param("id")
    var student models.Student
    result := initializers.DB.First(&student, id)

    if result.Error != nil {
        c.Status(400)
        return
    }
    student.ClassID = 0

    initializers.DB.Save(&student)

    c.JSON(200, gin.H{
        "message": "Student removed from class",
    })
}   

Even when adding a pointer to the ClassID it still doesn't work.

The error that it get :

Gorm How to set FK field back to null

And on the frontend :

Gorm How to set FK field back to null

The document you expected this should be explained

Expected answer

Clearing the ClassID field to null

Comment From: wolfwlj

My apologies for this issue, it seems it had more to do with Gin.

When I added a body to the function it and passed a = null from the frontend it worked.

func RemoveStudentFromClass(c *gin.Context) {
    id := c.Param("id")

    var body struct {
        ClassID     *uint 
    }
    c.Bind(&body)
    var student models.Student
    result := initializers.DB.First(&student, id)

    if result.Error != nil {
        c.Status(400)
        return
    }
    student.ClassID = body.ClassID
    initializers.DB.Save(&student)

    c.JSON(200, gin.H{
        "class": student,
        "message":  "student succesfully updated",
    })
}   

Frontend : ``Javascript async removeStudent (ID) { axios.put(http://localhost:9090/api/v1/adminportaal/removestudent/${ID}`, { ClassID: null // added this },

        {withCredentials: true}) 
        .then(response => {
            console.log(response)
            if(response.data.message == "Student removed from class"){
                this.getStudentsInClass()    
            }

        })
        .catch((err) => {
            console.log(err)
        })
    },

``