Cause: In the SlugModel.save() code that saves the object twice, if the force_insert flag was passed, it will also be passed on to the second save() call, even though by this point the object is assumed to be created. This inadvertently forces the existing object to be saved with an INSERT instead of an an UPDATE, which causes the integrity error above.
This PR clears the force_insert flag for the second save, which should avoid the problem.
It also adds some basic regression tests for the affected models: let me know if those look okay.
Currently, saving any SlugModel subclass with
save(force_insert=True)
will crash with a database-level IntegrityError.This happens when using many Django APIs that internally force insertion, such as
City.objects.create(…)
:Cause: In the
SlugModel.save()
code that saves the object twice, if theforce_insert
flag was passed, it will also be passed on to the secondsave()
call, even though by this point the object is assumed to be created. This inadvertently forces the existing object to be saved with anINSERT
instead of an anUPDATE
, which causes the integrity error above.This PR clears the
force_insert
flag for the second save, which should avoid the problem. It also adds some basic regression tests for the affected models: let me know if those look okay.