Software Entomology: Django Primary Key IntegrityError on Object Creation

Image inspired by this

About this Post

Briefly

Context

psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
DETAIL: Key (id)=(7) already exists.

Issue

Problem

Solution

What Happened?

A Peek into Perdition

Some Context

With great growth comes a greater strain on the software — Uncle Ben… probably

What Went Wrong?

This was all that was going through my head at the time. Credit goes to the Dilbert comic.
psycopg2.IntegrityError: duplicate key value violates unique constraint "doctors_doctor_pkey"
DETAIL: Key (id)=(7) already exists.

Why this is Insane!

What was the Problem?

Some Clues

Eureka!

The Solution

use_custom_manager = Truedef get_queryset():
if use_custom_manager:
do_my_own_magix()
if not use_custom_manager:
do_django_magix()
# Django save for Doctor object
def super_doctor_save():
# does some magic...
get_queryset()
# Our own save with our own alterations
def doctor_save():
use_custom_manager = False
super_doctor_save()
use_custom_manager = True
Example of with block, straightforward.

That’s it?!

The simpler the problem, the more complicated is the solution. The opposite is also true.

Out here developing