Skip to content

Commit

Permalink
Fix "sql no rows" error on admin user update (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
nt0xa authored Feb 15, 2023
1 parent 47e1851 commit 00a42cb
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions internal/database/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (db *DB) UsersCreate(o *models.User) error {
}

for _, f := range structs.Fields(o.Params) {
// Filter zero values here, because if for example "telegram" module is disabled we will have
// conflict error for all users with telegram id 0.
// Filter zero values here, because if for example "telegram" module is disabled we will have
// conflict error for all users with telegram id 0.
if !f.IsZero() {
if err := tx.Exec(
"INSERT INTO user_params (user_id, key, value) "+
Expand Down Expand Up @@ -113,14 +113,20 @@ func (db *DB) UsersUpdate(o *models.User) error {
"UPDATE users SET name = :name, is_admin = :is_admin, created_by = :created_by WHERE id = :id", o)

if err != nil {
tx.Rollback()
return err
}

for _, f := range structs.Fields(o.Params) {
if err := tx.Exec(
"UPDATE user_params SET value = $1 WHERE user_id = $2 AND key = $3",
f.Value(), o.ID, f.Tag("json")); err != nil {
return err
// Filter zero values here, because if for example "telegram" module is disabled we will have
// conflict error for all users with telegram id 0.
if !f.IsZero() {
if err := tx.Exec(
"UPDATE user_params SET value = $1 WHERE user_id = $2 AND key = $3",
f.Value(), o.ID, f.Tag("json")); err != nil {
tx.Rollback()
return err
}
}
}

Expand Down

0 comments on commit 00a42cb

Please sign in to comment.