Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » IntegrityError on unique_together constraint, save() use INSERT instead of UPDATE [RSS Feed]

#1 March 25, 2008 14:15:04

Malcolm T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

IntegrityError on unique_together constraint, save() use INSERT instead of UPDATE


On Tue, 2008-03-25 at 05:51 -0700, sector119 wrote:

> I try to write my own comand to manage.py which imports data in some
> format to the db.
> I write my own serializer which yields
> base.DeserializedObject(self.model(**data), m2m_data).
>
> When I run deserlialized object save method I got IntegrityError -
> because I use unique_together = ('location', 'pid') and model's data
> violate unique constraint "people_person_location_id_key".
> But I think that does Django have to run UPDATE not INSERT here?

Firstly, Django only does an update if your model has an existing pk
value *and* that primary key value exists in the database. On the model
you've provided, neither pid, nor location are the primary key of the
model.

Secondly, you've added an constraint that says "amongst all the rows in
these tables, these two values must be unique as a pair". It doesn't say
"and update the corresponding row if they already exist", which is what
you're hoping would happen. That's because it is *not* the same row
(different primary key values mean different rows).

I think the solution here is that you need to fix the data you're
submitting to not include constraint-busting data, or else subclass the
deserializer and override the saving code to catch the error, delete the
existing element and resave.

Regards,
Malcolm

--
A clear conscience is usually the sign of a bad memory.http://www.pointy-stick.com/blog/--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

#2 March 25, 2008 14:39:22

Malcolm T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

IntegrityError on unique_together constraint, save() use INSERT instead of UPDATE


On Tue, 2008-03-25 at 06:24 -0700, sector119 wrote:
> My models has primary key it's 'id' field and this field is
> prepopulated in Person.save() method
>
> def _get_id(self):
> return int(str(self.pid)+str(self.location.id))
>
> def save(self, raw=False):
> self.id = self._get_id()
> super(Person, self).save(raw=raw)
>
> But in:
> class DeserializedObject(object):
> def save(self, save_m2m=True):
> models.Model.save(self.object, raw=True)
>
> django call models.Model.save() method not Person.save() where my
> primary key is populated, and that is why primary key is None and
> django doesn't use UPDATE I think...

Deserializing data is for loading data that has already been saved and
serialized. So it will already have all the attributes populated
(including the primary key value). We intentionally avoid calling any
overridden save() methods to avoid unwanted side-effects and duplicate
processing.

You are using deserializing to also do some extra processing, which is
not the intended use-case. If you really want to do that, it will
involved subclassing the deserializer and writing your own save method
on that, too. You're not just doing simple deserialization here, you're
wanting to do loading *and* processing before saving, so you need to
subclass the standard behaviour to do what you want.

Regards,
Malcolm

--
Two wrongs are only the beginning.http://www.pointy-stick.com/blog/--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

  • Root
  • » Django
  • » IntegrityError on unique_together constraint, save() use INSERT instead of UPDATE [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support