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

Consider using natrual keys on the Election model #158

Open symroe opened 6 years ago

symroe commented 6 years ago

In some situations the sync_elections command can fail because it uses pk look ups at the same time as election_id being unique.

The root problem is some other process failing, but it might be worth trying Django's natural_keys to prevent the importer failing for this class of data consistency problem.

To do this we would need to implement natural_key() on the model and get_by_natural_key() on the manager.

symroe commented 6 years ago

Alternatively, convert election_id to be the PK on the Election model.

chris48s commented 6 years ago

Now we don't need the temp ids, I think it makes more sense to make election_id the PK. Note that this major DB change will require us to re-deploy any 'client' installs at the same time as the main instance.

chris48s commented 6 years ago

Had a really quick look at this to see if we can just change it and rely on django unicorns to magically sort it out. tl;dr: everything broke.

It seems that if you change the PK on a table, django doesn't automatically update FK fields. Fortunately I think the only FK we have to election_id is group_id but it looks like we need to write a series of manual migrations to do something like (pseudo-code):

err.. needs some testing