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.

3
  • 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. Commented Nov 7, 2022 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 Commented Nov 7, 2022 at 11:59
  • 1
    stackoverflow.com/a/15950324 Commented Nov 7, 2022 at 12:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.