Hey,
I get a weird error trying to save related objects. Basically documents related to a model. I have a Postgres backend, and seems either the postgres backend or the django orm is confused for some reason.
I used to be able to save this model fine:
class DocumentProduit(models.Model):
fk_produit = models.ForeignKey(Produit, on_delete=models.SET_DEFAULT,related_name="documents", null=False, blank=False, default=0)
I did drop & restaured the db (using pg_dumps) to some previous version. Since then (I think) I get this error trying to save my formsets:
django.db.utils.IntegrityError: duplicate key value violates unique constraint products_documentproduit_pkey" DETAIL: Key (id)=(21) already exists.
If I try it again, I get the same error message, but with Key (id)=(22)
instead (the duplicated ID increases by 1 each time).
My conclusion is that for some reason, the latest id / head of the postgres table (DocumentProduit) is wrong somewhere. Instead of just finding that the latest object in DB is id=157, it somehow attempts to save it as id=1, id=2, etc… Does django keep track of that? Or is my issue rather with postgres itself? Any idea how restauring the database may have caused this issue?