Skip to content

Can't create table; complains about unique index not existing, despite the field being present #55

@nhubbard

Description

@nhubbard

Hello all,

I'm new to TimescaleDB, but returning to Django after some time.

I'm trying to create a table based on the following models:

class Event(models.Model):
    # unique_id = models.UUIDField(primary_key=True, default=uuid.uuid4, null=False)
    time = TimescaleDateTimeField(interval="1 day")
    event_id = models.IntegerField(unique=True, null=False)
    event_detection_class_count = models.JSONField(null=False)
    event_alias_name = models.CharField(default=None, null=True)

    objects = models.Manager()
    timescale = TimescaleManager()


class Frame(models.Model):
    # unique_id = models.UUIDField(primary_key=True, default=uuid.uuid4, null=False)
    time = TimescaleDateTimeField(interval="1 day")
    camera_index = models.IntegerField(null=False)
    frame_url = models.URLField(null=False)
    frame_inference_results = models.JSONField(null=False)
    archived = models.BooleanField(default=False)

    objects = models.Manager()
    timescale = TimescaleManager()

However, no matter what I do, I get the following error:

psycopg2.DatabaseError: cannot create a unique index without the column "time" (used in partitioning)
HINT:  If you're creating a hypertable on a table with a primary key, ensure the partitioning column is part of the primary or composite key.

I found another issue that mentioned this issue, but the solution from that issue didn't work. Is this supposed to happen with a brand-new TimescaleDB installation? Am I doing something wrong with my models?

I added a print statement before it runs the SQL code, and got this SQL query in response:

CREATE EXTENSION IF NOT EXISTS timescaledb

SELECT
                c.relname,
                CASE
                    WHEN c.relispartition THEN 'p'
                    WHEN c.relkind IN ('m', 'v') THEN 'v'
                    ELSE 't'
                END,
                obj_description(c.oid, 'pg_class')
            FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind IN ('f', 'm', 'p', 'r', 'v')
                AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
                AND pg_catalog.pg_table_is_visible(c.oid)
        
  CREATE TABLE "api_event" ("id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, "time" timestamp with time zone NOT NULL, "event_id" integer NOT NULL UNIQUE, "event_detection_class_count" jsonb NOT NULL, "event_alias_name" varchar NULL)
DO $do$ BEGIN IF EXISTS ( SELECT * FROM timescaledb_information.hypertables 
    WHERE hypertable_name = 'api_event') THEN RAISE EXCEPTION 'assert failed - ''api_event'' should not be a hyper table'; ELSE NULL; END IF;END; $do$
ALTER TABLE "api_event" DROP CONSTRAINT "api_event_pkey"
SELECT create_hypertable('api_event', 'time', chunk_time_interval => interval '1 day', migrate_data => false)

The table is created successfully, but the call to create_hypertable is failing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions