0

I have a Django web application which is synced to a PostgreSQL database. After adding some rows to a couple of tables (using pgAdmin, not the Django application itself) i started experiencing some problems with the application. For example, i would get the following error in some tables:

Duplicate key value violates unique constraint "some_key_pkey" DETAIL: Key (id)=736284 already exists.

My assumption is that i had to let the Django application know somehow that i have added new rows to the database.

Could that really be the case? If so, how can i solve it?

Alasdair
  • 298,606
  • 55
  • 578
  • 516
gambit2017
  • 277
  • 1
  • 3
  • 11
  • No, that is not at all the case. However without seeing any code or the full traceback it is impossible to tell what is actually wrong. – Daniel Roseman Oct 10 '17 at 12:57
  • It sounds like you might need to reset the sequences. See [this question](https://stackoverflow.com/questions/244243/how-to-reset-postgres-primary-key-sequence-when-it-falls-out-of-sync). – Alasdair Oct 10 '17 at 13:07
  • DanielRoseman - i pasted the code of one error. @Alasdair - yes i'm aware of that, but it only started happening after i added rows manually to the database. What can i do to prevent it from happening again? – gambit2017 Oct 10 '17 at 13:50
  • If you add rows with ids that are higher than the sequence, then you simply need to reset the sequence to avoid these errors. Alternatively, you might not need to keep the ids when you copy data across. If you don't copy across the ids, then your database will assign new ids that don't clash. – Alasdair Oct 10 '17 at 14:07
  • Yes i was adding ids that were higher than the sequence. So that just means that that error was inevitable and i would always have to reset the sequence when i do that? – gambit2017 Oct 10 '17 at 14:21
  • Yes, that's right. – Alasdair Oct 10 '17 at 14:45

0 Answers0