bmc_hub/app/create_relation_table.py

29 lines
760 B
Python
Raw Permalink Normal View History

from app.core.database import get_db_connection, init_db, release_db_connection
init_db()
sql = """
CREATE TABLE IF NOT EXISTS case_location_relations (
id SERIAL PRIMARY KEY,
case_id INTEGER NOT NULL REFERENCES sag_sager(id) ON DELETE CASCADE,
location_id INTEGER NOT NULL REFERENCES locations_locations(id) ON DELETE CASCADE,
relation_type VARCHAR(50) DEFAULT 'located_at',
created_at TIMESTAMP DEFAULT NOW(),
deleted_at TIMESTAMP,
UNIQUE(case_id, location_id)
);
"""
conn = get_db_connection()
try:
with conn.cursor() as cursor:
cursor.execute(sql)
conn.commit()
print("Table created.")
except Exception as e:
conn.rollback()
print(f"Error: {e}")
raise
finally:
release_db_connection(conn)