1

So I have a Django model that stores 5 days worth of data in it because the amount of data is so large we must delete all data older than 5 days. The model currently has the auto incremented id field that Django creates automatically. The problem here is that pretty soon it won't be able to generate primary keys large enough.

Ideally, I'd have a composite primary key, but Django doesn't support this yet. So I was looking at unique_together and wondering if it was possible to just use that as a pesudo pk and remove the auto incrementing id because its not really used for anything in the application.

Another option is this module: django-compositekey but I'm not to sure how well its supported?

In either case the I would need to combine 4 columns to make a unique record:

class MassObservations(models.Model):
    time = models.DateTimeField()
    star = models.ForeignKey('Stars')
    property = models.ForeignKey('Properties')

    observatories = (('1', 'London'),
                     ('2', 'China'),
                     ('3', 'United States'))

    station = models.CharField(max_length=2, choices=observatories)
    mass = models.FloatField()

    class Meta:
        unique_together = ('time', 'star', 'property', 'station')

Any other ideas on how to treat data/Django table like this?

moku
  • 4,099
  • 5
  • 30
  • 52

2 Answers2

2

instead of using an integer as Primary key, you can use a UUID like this:

import uuid
from django.db import models

class MassObservations(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    ... rest of your fields ...

Conflicts are "unlikly" even with your number of records.

No primary key in general is "bad". Why? Just think about how you delete a single record.

Alternativly, you can create a custom "bigint" Primary key, like explained here:

Django BigInteger auto-increment field as primary key?

(Very unlikly that you run out of numbers soon :D)

Community
  • 1
  • 1
chickahoona
  • 1,914
  • 14
  • 23
1

I am not use id for primary key. Instead, I use a composite key, you can write like this:

class User(models.Model):
    a = models.IntegerField(primary_key=True)
    b = models.IntegerField(primary_key=True)
    c = models.IntegerField(primary_key=True)

So, when you use User.objects.get(...),you will not look this error:

OperationalError: (1054, "Unknown column 'user.id' in 'field list'")

Matthijs
  • 2,483
  • 5
  • 22
  • 33
Junjie Lee
  • 11
  • 1