0

I have a legacy Django Application (1.11) which uses UUID as primary key, hence doesn't have an ID field. My DB is postgres

class model_name(models.Model):
.... #model data ...

    companyId = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

.... # Other model data

I now need to add a human readable auto-incrementing identifier which will be used by users to refer to the records outside the system.

The challenges are as follows:

  • The system can have multiple entries being added simultaneously, so it needs to manage that without collisions
  • ideally would need to add this to the save() method because new
  • I have around 20000 records already in the database for this model. So whatever method I use, I will need to be able to add values for all previous records.
  • I need to start numbering at 100,000

Is there a way to do this (Autofield may not work because its not going to be primary key and i already have n records in the system.) so it will remain consistent?

kmcodes
  • 807
  • 1
  • 8
  • 20
  • 1
    You could look at [django-sequences](https://pypi.org/project/django-sequences/). – Alasdair Oct 09 '19 at 13:15
  • Which database are you using? – Alasdair Oct 09 '19 at 13:15
  • Postgres 9.6 is the database version – kmcodes Oct 09 '19 at 13:26
  • I checked out sequences as you suggested. I don't really mind gaps in the numbering as long as it doesn't get repeated. Also how do I add to previous values? Also in production the code gets redeployed (connecting to a remote DB) so will the sequences not start from one again? – kmcodes Oct 09 '19 at 13:31
  • That app uses [database sequences](https://www.postgresql.org/docs/9.6/sql-createsequence.html), so they won't reset when you restart the Django server. Before I saw that app, my first suggestion was to create a sequence. That and write something like `get_next_value` or modify the database schema to use the sequence as the default. Creating a sequence yourself means there can be gaps, but you've said that's not a problem. You will have to work out how to handle the migrations to create the sequence (and set the default value if applicable). – Alasdair Oct 09 '19 at 14:00
  • I tested it out actually while waiting for this reply. django-sequences dropped support for python 2.7 and doesnt work because of how they handle arguments (I know python 3 is the bomb and all, but for now I am stuck with 2.7 till I actually migrate to 3). Thanks for your support anyways. – kmcodes Oct 09 '19 at 14:04
  • Version [1.0.1](https://pypi.org/project/django-sequences/1.0.1/) supports Python 2.7 but doesn't explicitly support Django 1.11. If nothing else, the code might give you some ideas about how to roll your own solution that uses sequences in psql. – Alasdair Oct 09 '19 at 14:16
  • 1
    For now, planning to use epoch time as an incrementing seed for a simple integer field. Won't repeat and should meet my need till I figure out sequences. – kmcodes Oct 09 '19 at 18:31

0 Answers0