How to create conditional transactions?
Check your database's documentation on `SELECT FOR UPDATE` ([link to peewee docs](http://peewee.readthedocs.org/en/latest/peewee/api.html#SelectQuery.for_update)).

If you're using postgres, here are some docs. http://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-FOR-UPDATE-SHARE

Does this answer your question?
 Thank you very much for the information. This is a perfect answers to my question!

I have never worked with FOR UPDATE before, but I will read in the PostgreSQL documentation about it. Do I need any addon to peewee for this, or will it work with PostgreSQL out of the box?

Do you know if there is any peewee example code using for_update?
 It will work out of the box.  The tests are the only place there is example code for this feature, around line 3085.

Hello Coleifer. Thank you for creating so good software, we love using it!

Today we looked at code in which we need to make something like conditional transactions. I do not know if that is an official name, but for us this means that we want to check if a certain value is present in the database. If it is, we want to do some insert, and if it is not, we want to do a different insert. But in the time between the read and the insert, a different process could have inserted the searched for record, which would create inconsistencies.

My question being: we can include the select into the transaction like in the code below. This seems to work, but will that indeed make the select, the if statement, and the write all together atomic? In other wording; will this make it impossible to insert a user with username 'someUserName' between the read, and the write?

```
with db.transaction():
    result = User.select().where(User.username == 'someUserName')
    if result.count() == 0:
        insertObj = Something()
        insertObj.someattr = 123
        insertObj.save()
    elif result.count() == 1:
        insertObj = Something()
        insertObj.someattr = 456
        insertObj.save()
```

Also: is using result.count() the correct way to see if there is one occurrence, or is there a better/faster/more efficient way?
