Change find_or_create to do INSERT before SELECT

This makes find_or_create() safe to use with concurrent requests by
relying on unique indexes to raise errors if the INSERT fails. By doing
the SELECT first, we leave this method open to the same race conditions
that affect uniqueness validation.