GORM Playground Link

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

Description

Gorm version: 1.25.2

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!!

Comment From: ManoloTonto1

This was supposed to be a question not a bug(as of yet maybe?)