From 33d8baa8dd1fa79931ec390b83f384e72bf00705 Mon Sep 17 00:00:00 2001 From: grabowski Date: Tue, 11 Aug 2026 14:50:27 +0700 Subject: [PATCH] fix: composite (station_id, timestamp) PK on hii_* measurement tables TimescaleDB create_hypertable rejects tables whose primary key omits the partitioning column; the surrogate id PK served no purpose, so use the natural key directly. --- src/hii_collector.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/hii_collector.py b/src/hii_collector.py index 60dd93d..a097cda 100644 --- a/src/hii_collector.py +++ b/src/hii_collector.py @@ -204,13 +204,6 @@ class HiiStore: self.engine = None return False - def _pk(self) -> str: - if self.db_type == "sqlite": - return "INTEGER PRIMARY KEY AUTOINCREMENT" - if self.db_type == "postgresql": - return "BIGSERIAL PRIMARY KEY" - return "BIGINT AUTO_INCREMENT PRIMARY KEY" - def _create_tables(self): from sqlalchemy import text @@ -229,15 +222,16 @@ class HiiStore: updated_at TIMESTAMP ) """, - f""" + # Composite natural PK (no surrogate id): TimescaleDB hypertable + # conversion requires every unique index to include the time column. + """ CREATE TABLE IF NOT EXISTS hii_rainfall ( - id {self._pk()}, station_id INTEGER NOT NULL, timestamp TIMESTAMP NOT NULL, rain_1h NUMERIC(7,2), rain_24h NUMERIC(8,2), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT uq_hii_rainfall UNIQUE (station_id, timestamp) + PRIMARY KEY (station_id, timestamp) ) """, f""" @@ -261,9 +255,8 @@ class HiiStore: updated_at TIMESTAMP ) """, - f""" + """ CREATE TABLE IF NOT EXISTS hii_waterlevel ( - id {self._pk()}, station_id INTEGER NOT NULL, timestamp TIMESTAMP NOT NULL, wl_msl NUMERIC(8,3), @@ -274,7 +267,7 @@ class HiiStore: situation_level INTEGER, diff_wl_bank NUMERIC(8,3), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT uq_hii_waterlevel UNIQUE (station_id, timestamp) + PRIMARY KEY (station_id, timestamp) ) """, "CREATE INDEX IF NOT EXISTS idx_hii_rainfall_ts ON hii_rainfall(timestamp)",