timeflake icon indicating copy to clipboard operation
timeflake copied to clipboard

simple-django-history not compatible with TimeflakePrimaryKeyBinary

Open bcastorBTO opened this issue 3 years ago • 1 comments

Lets say you have the following model and want to add history tracking to each FooModel object:

from simple_history.models import HistoricalRecords

class FooModel(models.Model):
   id = TimeflakePrimaryKeyBinary()
   history = HistoricalRecords(
        history_id_field=models.UUIDField(default=uuid.uuid4)
    )

It generates the following error during makemigrations: venv/lib/python3.9/site-packages/timeflake/extensions/django/__init__.py", line 97, in deconstruct del kwargs["primary_key"] KeyError: 'primary_key'

However, I tried adding the following try/except around this code and the migration would generate without an issue: image

Downside is that leads to this error when trying to execute the migration: django.db.utils.ProgrammingError: multiple primary keys for table "requisition_data_historicalrequisitionmodel" are not allowed LINE 1: ...me" varchar(200) NULL, "history_id" uuid NOT NULL PRIMARY KE...

So at the end of the day any model that uses TimeflakePrimaryKeyBinary for a primary ky id field is unable to track object history using https://django-simple-history.readthedocs.io/en/latest/ which is a deficit.

I can manually adjust the migration after the fact to "work" (i.e. be a charfied or uuid field etc with primary_key = False) but django will not honor the change and all subsequent makemigrations commands will try to undo the changes.

Can this issue be investigated? Thanks!

bcastorBTO avatar Dec 01 '22 16:12 bcastorBTO

For anyone else using this, you can just use the TimeflakeBinary field with the args:

primary_key=True,
editable=False,
default=timeflake.random,

That should fix these issues.

bpeterman avatar May 19 '25 16:05 bpeterman