Issue: bug on rollback? (peewee==2.8.7)
Author: paulocheque
State: closed
Created: 2017-03-06 06:56:30

- Python 3.6
- peewee==2.8.7


Hi there, In the example below, two records (indexes 0 and 1) will be saved successfully in the database, although I am calling rollback.

```
db.connect()
with db.transaction() as txn:
    for index, row in enumerate(rows):
        try:
            model = MyModel()
            if index == 2:
                raise Exception('This should cancel the entire transaction, right?')
            model.save()
        except Exception as e:
            print('Rollbacking') # yes, lets rollback
            txn.rollback()
            raise # With this, no commit will be made
    print('Commiting') # not being here
    txn.commit()
db.close()
```

Is that correct?

If I'm understanding your "example" code, then the following is equivalent:

```pythondb = SqliteDatabase(':memory:')

class Register(Model):
    value = IntegerField()
    class Meta:
        database = db

db.create_tables([Register])

with db.transaction() as txn:
    Register.create(value=1)
    Register.create(value=2)
    txn.rollback()
    txn.commit()

print Register.select().count()
```

When I run that script, the output is `0` as I'd expect.

Did you test with PostgresqlExtDatabase (Postgres 9.6)? With Python 3.6? With peewee 2.8.7?

For me the output is `2`.

```
    from playhouse.postgres_ext import PostgresqlExtDatabase
    db = PostgresqlExtDatabase()
    db.connect()
    with db.transaction() as txn:
        # User(email='a@a.com').save()
        # User(email='b@a.com').save()
        User.create(email='a@a.com')
        User.create(email='b@a.com')
        txn.rollback()
        txn.commit()
    print(User.select().count())
    db.close()

```

Any ideas?

I did not test with Python 3. Maybe you can test with Python 2 and see if that works for you?

I re-ran the script and I'm still seeing 0 as the output:

```python
from peewee import *
from playhouse.postgres_ext import PostgresqlExtDatabase


#db = SqliteDatabase(':memory:')
db = PostgresqlExtDatabase('peewee_test')

class Register(Model):
    value = IntegerField()
    class Meta:
        database = db

db.create_tables([Register])

with db.transaction() as txn:
    Register.create(value=1)
    Register.create(value=2)
    txn.rollback()
    txn.commit()

print(Register.select().count())
db.drop_tables([Register])
```

Are you sure you don't have users in the db you're counting? Maybe drop then re-create the table?

I tested here a clean example with Py2 and Py3 and it is working. Well, no idea what is happening in my current project. Thanks for the help.

@paulocheque -- glad to hear it's working for you. One thing to also consider is the possibility you had nested `transaction()` blocks...Because the semantics of nested transactions may be confusing, I'd recommend using `atomic()` instead. By using `atomic()`, peewee will automatically use a transaction or savepoint where appropriate.

I understand now my mistake. I was using two different `PostgresqlExtDatabase` intances. One for the User model declaration and another one for the queries. Now I am using a single `db` variable and it is working. Thanks for the help again.