Currently Django performs get_or_create as a series of separate two calls:
1. try get
2. perform create if the DoesNotExist exception was thrown
This can lead to IntegrityError
exception being raised if the instance was created right after the DoesNotExist exception was thrown.
Is there any existing solution that allows to swap current django code for a postgres-specific single-query get_or_create
? No problem if the solution only works with postgres as the db backend. Bonus points if it allows async aget_or_create
as well.
I know this should be possible on posgres as in the accepted answer in this SO question: Write a Postgres Get or Create SQL Query But I want to look for existing solutions before customising the calls in my code one by one.