djangopostgresqltimescaledb

How to create a TimescaleDB Hypertable from Django models?


By defining a Django model, you can't create a hypertable in TimescaleDB, I got an error like: Cannot create a unique index without using the partitioning key with the following (example) model:

class Record(models.Model):
    class State(models.Choices):
        ONLINE = "online"
        OFFLINE = "offline"

    time = models.DateTimeField()

    mode = models.CharField(max_length=10, choices=Mode.choices)
    state = models.CharField(max_length=10, choices=State.choices)

Does anyone know how can I fix this?


Solution

  • Based on https://github.com/timescale/timescaledb/issues/447, this can be solved by defining the time field as the primary key.

    time = models.DateTimeField(auto_now_add=True, db_index=True, primary_key=True)