views:

54

answers:

2

I got a model TrackedItem with a generic relation linking to any model it is supposed to track.

If I do that:

t = TrackedItem(content_object=MyModel)
t.save()
t.save()

I get :

IntegrityError: (1062, "Duplicate entry '1' for key 'PRIMARY'")

Indeed, the first save has created an entry with "1" as a PK. But the second save should not insert, it should update.

How am I suppose to update a model I can't save twice?

With an ordinary model I can save as much as I want.

EDIT : it may be not related at all with generic relations.

I'm having a overrided save, and I call super in it, this way :

super(TrackedItem, self).save(self, *args, **kwargs)

if I do it this way, it works:

model.Model.save(self, *args, **kwargs)
A: 

I assume this is a DB transaction issue. Is there a DB-Commit bewteen the two save calls? Maybe your View is under transaction.commit_on_sucess controll.

2 Possibilities:

  transaction.commit() # within transaction.commit_manually

or

  t.save(force_update=True) # 2nd save call
maersu
No, it's a very basic code, I'm not using commit anywhere. Plus, grepping the lib I use don't show me any "commit". I tried "force_update" but end up with "ValueError: Cannot force both insert and updating in model saving."
e-satis
OK,then its porbably i failure within your custom save (like Botondus said). Have a look at your custom save written in the question. Their was a wrong 'self'.it schould be in the form like:super(TrackedItem, self).save(*args, **kwargs)
maersu
+3  A: 

Your problem is most likely because of wrong use of 'super'. It should be like this:

super(TrackedItem, self).save(*args, **kwargs)
Béres Botond