SQLAlchemy and Race Conditions! Implementing `get_one_or_create()`

03:00 PM - 03:30 PM on August 16, 2014, Room 702

Erik Taubeneck

Audience level:


Implementing get_one_or_create() for SQLAlchemy.


If you've ever played around with Django, you've probably seen the get_or_create() function, but if not the concept is straightforward. First look for an object given a set of constraints, and if it doesn't exist, create it. Seems simple enough, and we'll work through building this function out to take advantage of some of Python's more advanced featured, and eventually, we'll hit a race condition! The hidden complexity of the issues at work here will be discussed (related to why SQLAlchemy doesn't have this built in), and propose a solution.

This will be based on my blog post on the subject (http://skien.cc/blog/2014/01/15/sqlalchemy-and-race-conditions-implementing/). There are also a couple follow up posts, but they will only be used with respect to changing the end solution slightly.