saltbo / zpan

A self-hosted cloud disk base on the cloud storage./ 一个基于云存储的网盘系统,用于自建私人网盘或企业网盘。
https://zpan.space
GNU General Public License v3.0
1.84k stars 239 forks source link

fix: storage update error #139

Closed 2hxst closed 3 years ago

2hxst commented 3 years ago
func (s *Storage) Update(id string, storage *model.Storage) error {
    existStorage := new(model.Storage)
    if err := gdb.First(existStorage, id).Error; errors.Is(err, gorm.ErrRecordNotFound) {
        return fmt.Errorf("storage not found")
    }
    storage.Id, _ = strconv.ParseInt(id, 10, 64)
    storage.Created = existStorage.Created
    // 如果SK没有发生改变则不允许更新SK,避免改错SK
    if storage.SecretKey == existStorage.SKAsterisk() {
        storage.SecretKey = existStorage.SecretKey
    }
    return gdb.Save(storage).Error
}

api: PUT /api/storages/:id error: Incorrect datetime value: '0000-00-00' for column 'created' at row 1

解决方案1:如上, 将existStorage中的Created赋值到model.Storage对象中 解决方案2:pkg/bind/storages.go 对象 StorageBody 添加绑定字段Created

个人更倾向于直接修改existStorage,从model.Storage读取可修改字段赋值至existStorage

saltbo commented 3 years ago

你用的是什么数据库,哪个版本? 这个问题应该改下created的tag标签就好了,只允许读取不允许写入

saltbo commented 3 years ago

https://gorm.io/docs/models.html#Field-Level-Permission

2hxst commented 3 years ago

你用的是什么数据库,哪个版本? 这个问题应该改下created的tag标签就好了,只允许读取不允许写入

MySQL 5.7

saltbo commented 3 years ago

我用的5.6 你试下给Model Storage的created加个限制只读的tag

https://gorm.io/docs/models.html#Field-Level-Permission

2hxst commented 3 years ago

我用的5.6 你试下给Model Storage的created加个限制只读的tag

https://gorm.io/docs/models.html#Field-Level-Permission

添加tag gorm:"->;<-:create" 是可以的

saltbo commented 3 years ago

好的,可以更新下PR

saltbo commented 3 years ago

不用关闭啊, force-push会自动更新到当前PR的