DemocracyClub / EveryElection

:ballot_box_with_check: For recording every election in the UK
https://elections.democracyclub.org.uk/
BSD 3-Clause "New" or "Revised" License
11 stars 14 forks source link

Duplicate key error when creating more than one election ID #115

Closed symroe closed 6 years ago

symroe commented 6 years ago

Reproduce by:

  1. Making more than one new election of the same type on the same day in a single wizard flow. e.g, enter a date > local elections > 2 councils > some wards > create ID.

You'll then get something like:

duplicate key value violates unique constraint "elections_election_election_id_731f3058_uniq"
DETAIL:  Key (election_id)=(local.2018-01-01) already exists.

This is because save() is being called on both the [type].[date] group ID objects. The first one works, but the second doesn't because it's trying to save a model with a PK that already exists.

Related to this, note that on the review step there's more than one group ID listed.

chris48s commented 6 years ago

Not sure exactly how, but I'm guessing I messed this up in #108 when I moved the get_or_create() logic that is supposed to prevent this condition to here:

https://github.com/DemocracyClub/EveryElection/blob/master/every_election/apps/elections/models.py#L146-L149

in 2760de6