7

I try to update the PK in Django with the .save() method, but when I save the object, Django duplicates the object with the same data but a different PK. For example:

from gestion_empleados.Models import Empleados
>>> e = Empleados.objects.get(pk="56789034U")
>>> e.pk
u'56789034U'
>>> e.pk = "11111111L"
>>> e.save()
>>> e.pk
'11111111L'
>>> e2 = Empleados.objects.get(pk="56789034U")
>>> e2
<Empleados: Juan 56789034U>
>>> e
<Empleados: Juan 11111111L>

The objects are the same with different PKs, and I want to change the PK without duplicating the object.

Any solution? Thanks!

3 Answers 3

12

I don't think Django allows you to change the object's primary key. You may have to delete the original object.

e2.delete()

According to the Django docs

The primary key field is read-only. If you change the value of the primary key on an existing object and then save it, a new object will be created alongside the old one.

Django Docs

1
  • I think to delete the original object but is strange that django don´t permit update PK, thanks!
    – avr
    Apr 15, 2015 at 18:19
3

Django's Model.save() method relies on whether there's already a row with the same PK in your db to decide if it should issue an INSERT or UPDATE query.

As a more general rule: while it's technically possible to modify a PK at the SQL level, it's no necessarily such a good idea, as it means you'd have to update all related rows in all related tables (ok, still technically possible but really not a sane idea as far as I'm concerned), AND warn all applications depending on this PK of the change too - and then good luck. To make a long story short: it's always safer to consider PKs as immutable (and that's why quite a few people in the SQL world favor surrogate primary keys even when there's a seemingly obvious natural one).

9
  • 2
    The problem here is that Django tries to be DB-agnostic, and hence doesn't support CASCADE UPDATE, which is the sane way to update a natural key.
    – DylanYoung
    Nov 4, 2016 at 15:39
  • 2
    @DylanYoung as far as I'm concerned the only "sane" way is to have immutable, non reusable primary keys. Try a cascade update on real-life sized dataset and you'll find out why it's not a viable solution. Also, it will only update the database content itself, not any external data that depend on it (and it's a very common situation to have external apps depending on your database pks). Jan 11, 2019 at 12:17
  • 1
    Thank your stars you aren't managing a database that needs to migrate primary keys then :)
    – DylanYoung
    Jan 14, 2019 at 17:52
  • FYI: every database I know reuses serial primary keys. Using UUIDs can have a substantial performance impact. Is that sane?
    – DylanYoung
    Jan 14, 2019 at 17:54
  • 1
    If you're talking about deep refactoring / restructuration, moving data from one system to another (with of course totally incompatible schema) etc then of course there are reasons to mess with primary keys here - and I wrote my own share of (sometimes very complex) data migration scripts. What I meant is that the only reasons for primary key changes were purely technical and only the domain of such migration scripts - the schema itself should not mandate it (pks should be choosen so they should not have to change for business domain reasons) and application code has no business changing PKs. Jan 15, 2019 at 15:24
0

First you should make sure that the object with the primary key "11111111L" has been added to your table. Probably doing something along the lines of:

e3 = Empleados.objects.get(pk="11111111L")

And then making sure that e3 contains . Once you confirm that it is there, then you can just use the following statement to get rid of the object with the primary key "56789034U" (assuming you keep e2 around):

e2.delete()

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.