django not raising IntegrityError for duplicate primary key
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC E Schuberts Piano Sonata D 784 in A
--
Chapters
00:00 Question
01:10 Accepted answer (Score 3)
01:47 Thank you
--
Full question
https://stackoverflow.com/questions/1446...
Question links:
[here]: https://docs.djangoproject.com/en/dev/re...
Accepted answer links:
[django knows whether to UPDATE or INSERT]: https://docs.djangoproject.com/en/dev/re...
[the documentation]: https://docs.djangoproject.com/en/dev/to...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #djangotesting
#avk47
--
Track title: CC E Schuberts Piano Sonata D 784 in A
--
Chapters
00:00 Question
01:10 Accepted answer (Score 3)
01:47 Thank you
--
Full question
https://stackoverflow.com/questions/1446...
Question links:
[here]: https://docs.djangoproject.com/en/dev/re...
Accepted answer links:
[django knows whether to UPDATE or INSERT]: https://docs.djangoproject.com/en/dev/re...
[the documentation]: https://docs.djangoproject.com/en/dev/to...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #djangotesting
#avk47
ACCEPTED ANSWER
Score 3
Your test method is wrong. What you're doing here is updating the existing instance since you're supplying an already used primary key. Change the save to a force_insert like so.
def test_unique_id(self):
with self.assertRaises(IntegrityError):
badSite = Site(id=0, name='Bad Site')
badSite.save(force_insert=True)
badSite.delete()
The django docs explain how django knows whether to UPDATE or INSERT. You should read that section.
Are you aware that django already supports automatic primary keys? See the documentation for more of an explanation.