use conditional INSERT instead of nested transactions in lazy_create

This lets us avoid several classes of race conditions when concurrent
transactions invoke lazy_create (the bug report has the gory details).
As an added bonus, it should improve performance due to less SQLAlchemy
object expiry.

Unfortunately it makes the implementation of lazy_create a bit
trickier...

Bug: 874386
Change-Id: I5b4a1ce3b59f82003e8c2983c5d2ca66f20fc2d6
Merge "use conditional INSERT instead of nested transactions in lazy_create" into develop