mirror of
https://github.com/photoprism/photoprism.git
synced 2026-01-23 02:24:24 +00:00
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
parent
a542e15ae8
commit
be00bcf0b3
1 changed files with 13 additions and 9 deletions
|
|
@ -4,12 +4,13 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
// Update updates an existing record in the database.
|
||||
// Update updates the values of an existing database record.
|
||||
func Update(m interface{}, keyNames ...string) (err error) {
|
||||
// Unscoped so soft-deleted records can still be updated.
|
||||
// Use an unscoped *gorm.DB connection, so that
|
||||
// soft-deleted database records can also be updated.
|
||||
db := UnscopedDb()
|
||||
|
||||
// New entity?
|
||||
// Return if the record has not been created yet.
|
||||
if db.NewRecord(m) {
|
||||
return fmt.Errorf("new record")
|
||||
}
|
||||
|
|
@ -17,20 +18,23 @@ func Update(m interface{}, keyNames ...string) (err error) {
|
|||
// Extract interface slice with all values including zero.
|
||||
values, keys, err := ModelValues(m, keyNames...)
|
||||
|
||||
// Has keys and values?
|
||||
// Check if the number of keys matches the number of values.
|
||||
if err != nil {
|
||||
return err
|
||||
} else if len(keys) != len(keyNames) {
|
||||
return fmt.Errorf("record keys missing")
|
||||
}
|
||||
|
||||
// Update values.
|
||||
// Execute update statement.
|
||||
result := db.Model(m).Updates(values)
|
||||
|
||||
// Successful?
|
||||
if result.Error != nil {
|
||||
// Return an error if the update has failed.
|
||||
if err = result.Error; err != nil {
|
||||
return err
|
||||
} else if result.RowsAffected > 1 {
|
||||
}
|
||||
|
||||
// Verify number of updated rows.
|
||||
if result.RowsAffected > 1 {
|
||||
log.Debugf("entity: updated statement affected more than one record - you may have found a bug")
|
||||
return nil
|
||||
} else if result.RowsAffected == 1 {
|
||||
|
|
@ -39,5 +43,5 @@ func Update(m interface{}, keyNames ...string) (err error) {
|
|||
return fmt.Errorf("record not found")
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue