Your Question

Hello there, I cannot make this work for some reason, i have checked some issues and it looks like the unscoped function does not make my query work as expected.

here i want to delete an entry in my many2many table association.

type Calendar struct {
    UUID         uuid.UUID `gorm:"primaryKey;type:uuid;default:uuid_generate_v4()"`
    Name         string
    CalendarLink string
    EmployeeID   uint
    Private      bool
    Type         CalendarType
}

type EmployeeCalendarSubscriptions struct {
    UserID    uint       `gorm:"primaryKey"`
    Calendars []Calendar `gorm:"many2many:employee_calendar_subscriptions_calendars;"`
}

func SubscribeToCalendar(userId uint, calendarUUID uuid.UUID) error {
    db := database.ConnectToDatabase()

    calendar := Calendar{
        UUID: calendarUUID,
    }

    err := db.Model(&EmployeeCalendarSubscriptions{
        UserID: userId,
    }).
        Association("Calendars").
        Unscoped().
        Append(&calendar).Error()
    if len(err) > 0 {
        return errors.New(err)
    }
    return nil

}

func UnsubscribeFromCalendar(userId uint, calendarUUID uuid.UUID) error {
    db := database.ConnectToDatabase()
    calendarTobeDeleted := Calendar{
        UUID: calendarUUID,
    }
    err := db.Model(&EmployeeCalendarSubscriptions{
        UserID: userId,
    }).
        Association("Calendars").
        Unscoped().
        Delete(&calendarTobeDeleted).Error()
    log.Println(err)
    if len(err) > 0 {
        return errors.New(err)
    }
    return nil
}

error: runtime error: invalid memory address or nil pointer dereference Running any of these functions gives me a nil pointer dereference. which is obvious why, because the struct does not represent the data structure saved in Postgres. In my mind, I think that if I used Unscoped it will not write to the memory address of calendarTobedeleted or the other variable: calendar in SubscribeToCalendar.

Maybe i am doing something wrong? Anything helps!!

The document you expected this should be explained

Expected answer

How to avoid a pointer dereference.

Comment From: gileez

hit the same issue. its crazy how such a simple concept doesnt have a clear answer in the docs or anywhere online

Comment From: jinzhu

Have you read our document? I think it's pretty clear

https://gorm.io/docs/associations.html#Delete-Association-Record

https://gorm.io/docs/associations.html#Delete-Associations

Comment From: gileez

hey @jinzhu i have read the documentation at length and agree with most of the comments i find online claiming that the documentation is very unclear. is it possible it is clear to you because you are the main author?

the documentation: - doesnt show the start \ end state of the db or clearly communicate what the effect will be - doesnt show the resulting sql query so i had to investigate this on my own. it doesnt even state when additional queries will be performed - doesnt state the returned object type (which i have found changes depending on what query i perform) - lots more....

after finding 3 ways in the docs to perform this deletion only 1 worked for me and its the one that uses the Select function to signify that i would like to delete the associated rows from the association table. I am new to GORM but not new to ORMs at all.

I appreciate the hard work but if the users are saying the documentation is unclear you should believe them cheers