diff --git a/models/class_board.go b/models/class_board.go index 068f1c1..83ed439 100644 --- a/models/class_board.go +++ b/models/class_board.go @@ -10,7 +10,7 @@ type ClassBoard struct { CreatedAt time.Time `gorm:"not null;"` UpdatedAt time.Time `gorm:"not null;"` IsAnnounced bool `gorm:"not null;default:false"` - CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` UID uint `gorm:"column:uid;not null"` // User ID Class Class `gorm:"foreignKey:CID;constraint:OnDelete:CASCADE"` User User `gorm:"foreignKey:UID"` diff --git a/models/class_code.go b/models/class_code.go index 123883b..27ab4e3 100644 --- a/models/class_code.go +++ b/models/class_code.go @@ -4,7 +4,7 @@ type ClassCode struct { ID uint `gorm:"primaryKey;size:255;autoIncrement"` Code string `gorm:"size:10;not null"` Secret *string `gorm:"size:20"` - CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` + CID uint `gorm:"column:cid;not null;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` UID uint `gorm:"column:uid;not null"` // User ID Class Class `gorm:"foreignKey:CID;constraint:OnDelete:CASCADE"` User User `gorm:"foreignKey:UID"` diff --git a/repositories/class_user_repository.go b/repositories/class_user_repository.go index 47ee768..1477d13 100644 --- a/repositories/class_user_repository.go +++ b/repositories/class_user_repository.go @@ -220,10 +220,16 @@ func (r *classUserRepository) RoleExists(uid uint, cid uint) (bool, error) { } func (r *classUserRepository) CreateUserRole(uid uint, cid uint, role string) error { + var user models.User + if err := r.db.First(&user, uid).Error; err != nil { + return err + } + newUserRole := models.ClassUser{ - UID: uid, - CID: cid, - Role: role, + UID: uid, + CID: cid, + Role: role, + Nickname: user.Name, } return r.db.Create(&newUserRole).Error }