fix: composite (station_id, timestamp) PK on hii_* measurement tables
CI/CD Pipeline - Northern Thailand Ping River Monitor / Test Suite (3.11) (push) Failing after 22s
CI/CD Pipeline - Northern Thailand Ping River Monitor / Build Docker Image (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Integration Test with Services (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Deploy to Staging (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Deploy to Production (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Performance Test (push) Skipped
CI/CD Pipeline - Northern Thailand Ping River Monitor / Code Quality (push) Successful in 12s
Documentation / Validate Documentation (push) Failing after 8s
Documentation / Generate API Documentation (push) Successful in 9s
Documentation / Build Sphinx Documentation (push) Successful in 17s
CI/CD Pipeline - Northern Thailand Ping River Monitor / Cleanup (push) Successful in 1s
Documentation / Documentation Summary (push) Successful in 3s

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.
This commit is contained in:
2026-08-11 14:50:27 +07:00
parent 1845ef7203
commit 33d8baa8dd
+6 -13
View File
@@ -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)",