Delete function affected rows count mistake #1386

Closed
opened 2019-08-06 09:55:37 +00:00 by Keleir · 1 comment
Keleir commented 2019-08-06 09:55:37 +00:00 (Migrated from github.com)

Sample Code:

func (u *UserController) DelUser (name string) error {
  user := new(models.User)
  filter := builder.Eq{"name": name}

  // * This code is just to debug whether the record exists
  total, err := database.DB.Where(filter).FindAndCount(user)
  log.Debugf("exists rows: %v", total)
  // * Debug Ended

  count, err := database.DB.Where(filter).Delete(user)
  log.Debugf("affect rows: %v", count)

  if count < 1 {
    return fmt.Errorf("record not found: %s", name)
  }

  return err
}
  • The log output is as following:
2019-08-06 17:27:26 [DEBUG] exists rows: 0
2019-08-06 17:27:26 [DEBUG] effect rows: 1

And this behavior is quite different from the mysql client when I execute the delete sql generated
by xorm debug log.

No errors; 0 rows affected, taking 8.3 ms

Expected Behavior:

The Delete func(bean interface{}) (int64, error) return the right affectedRows count,
e.g: when delete a not exists record, the return value int64 should be 0.

### Sample Code: ```go func (u *UserController) DelUser (name string) error { user := new(models.User) filter := builder.Eq{"name": name} // * This code is just to debug whether the record exists total, err := database.DB.Where(filter).FindAndCount(user) log.Debugf("exists rows: %v", total) // * Debug Ended count, err := database.DB.Where(filter).Delete(user) log.Debugf("affect rows: %v", count) if count < 1 { return fmt.Errorf("record not found: %s", name) } return err } ``` - The log output is as following: ```scala 2019-08-06 17:27:26 [DEBUG] exists rows: 0 2019-08-06 17:27:26 [DEBUG] effect rows: 1 ``` And this behavior is quite different from the mysql client when I execute the delete sql generated by xorm debug log. ```scala No errors; 0 rows affected, taking 8.3 ms ``` ### Expected Behavior: The `Delete func(bean interface{}) (int64, error)` return the right `affectedRows` count, e.g: when delete a not exists record, the return value int64 should be 0.
lunny added
duplicate
and removed
need
test
labels 2020-03-23 12:57:13 +00:00

I think this has been fixed.

I think this has been fixed.
lunny closed this issue 2020-03-23 12:57:23 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1386
No description provided.