0

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.

rioted
  • 1,076
  • 13
  • 24
  • A lot of things can result in an integrity error, this is usually why requests are wrapped in transactions, to rollback the entire transaction in case it fails. – Willem Van Onsem Nov 07 '22 at 11:49
  • This two operations are make into a single db transaction. so i think the conclusion is not good. You cannot get an integrity error for this reason – Lucas Grugru Nov 07 '22 at 11:59
  • 1
    https://stackoverflow.com/a/15950324 –  Nov 07 '22 at 12:33

0 Answers0