We need to replace the IntegerField Contribution.contributionnumber
Contribution models have a unique IntegerField
, contributionnumber
It is created like this:
contribution_max_nb = Contribution.objects.aggregate(Max('contributionnumber'))['contributionnumber__max']
if contribution_max_nb is None:
contribution_nb = 1
else:
contribution_nb = contribution_max_nb + 1
contributionInstance = instance.contributions.create(
...
contributionnumber = contribution_nb
)
contributionInstance.save()
The issue with this is that it's replicating the function of the primary key, and it has a race condition if I run this many times at once
The best solutions would be:
- To replace this with a
CharField
- we can then use the Python functionuuid.uuid4()
to create unique contribution numbers likec966e5e2-e4e9-4468-91f3-2f4cc2471c93
- To remove the
contributionnumber
field altogether and use the built-in primary key property (contribution.pk
)