Issue: on_conflict(update) doesn't return count of changed rows
Author: vladiscripts
State: closed
Created: 2020-02-08 14:15:44

The `Insert.execute()` and `Update.execute()` methods, when making changes, return non-zero values, according to the [help](http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.update). 
The `Insert.on_conflict().execute()` method works same.

But `Insert.on_conflict(update = ...).execute()` makes changes always returning zero. Example:

```python
changed = (Data
         .insert(date=date, pid=pid, value=value)
         .on_conflict(
             conflict_target=[Data.date, Data.pid],
             update={Data.value: 7})
         .execute())
```


Peewee 3.13.1, SQLite, Python 3.7

In this minimal test, using an auto-increment primary key, Peewee is returning the PK of the modified row:

```python

db = SqliteDatabase(':memory:')
# Or use postgres
# db = PostgresqlDatabase('peewee_test')

class Register(Model):
    key = TextField(unique=True)
    value = IntegerField()
    class Meta:
        database = db

db.create_tables([Register])

Register.create(key='k0', value=0)
iq = (Register
      .insert(key='k1', value=1)
      .on_conflict(conflict_target=[Register.key],
                   update={Register.value: Register.value + 1}))

print(iq.execute())
print(iq.execute())

for r in Register.select():
    print(r.key, r.value)
```

Prints the following for both Sqlite and Postgres:

```
2
2
k0 0
k1 2
```

Which is what I'd expect.

On same schema:

```python
data = {'k0':0, 'k1':1, 'k2':2}

for k, v in data.items():
    iq = (Register
          .insert(key=k, value=v)
          .on_conflict_ignore()
          .execute())
    print(f'on_conflict_ignore change = {iq}')

print('table contains:')
for r in Register.select():
    print(r.key, r.value)

for k, v in data.items():
    iq = (Register
          .insert(key=k, value=v)
          .on_conflict(conflict_target=[Register.key],
                       update={Register.value: Register.value + 1})
          .execute())
    print(f'on_conflict_update change = {iq}')

print('table contains:')
for r in Register.select():
    print(r.key, r.value)
```

output:
```
on_conflict_ignore change = 1
on_conflict_ignore change = 2
on_conflict_ignore change = 3
table contains:
k0 0
k1 1
k2 2
on_conflict_update change = 3
on_conflict_update change = 3
on_conflict_update change = 3
table contains:
k0 1
k1 2
k2 3
```
All rows was changed. But return of `insert.on_conflict(update = ...).execute()` are alway 3 (`change = 3`). 

Sqlite is reporting the last insert rowid (3). These examples are not triggering the rows-modified branch, but the last-insert-rowid branch.

Sorry, I don't understand. How to get a sign that UPDATE has been done? In my case, it doesn’t matter if the return is the number of rows changed as stated in the `.update` documentation, or the PK of the modified row as you said above.

You mean how to differentiate between inserting a new row and updating an existing one?

Yes. In my case, tens of thousands of rows are regularly loaded into the table. And need to check if any rows have been updated. This is the trigger for executing a following program code.

You'll have to experiment with it -- as it is, I believe even if peewee returned you the `rows_affected`, it would still just be the total number of rows you insert/update.

That is, Peewee hasn't and haven't expect support for returning count_updated or last_modified_rowid for the `.on_conflict(update=)` method. It returns only the rowid of the last execution of insert. While [`Model.update()` returns](http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.update) the number of rows changed. Do I understand correctly?

There's no such thing as a last-modified rowid in Sqlite. Sqlite has last inserted id, and rows affected.