Search code examples
pythonsqlitepeewee

get_or_create in Peewee


The paragraph titled Get or create on the peewee documentation says:

While peewee has a get_or_create() method, this should really not be used outside of tests as it is vulnerable to a race condition. The proper way to perform a get or create with peewee is to rely on the database to enforce a constraint.

And then it goes on with an example that only shows the create part, not the get part.

What is the best way to perform a get or create with peewee?


Solution

  • Everything you are doing inside a transaction is atomic.

    So as long as you are calling get_or_create() inside a transaction, that paragraph is wrong.