fix: resolve foreign key constraint violation during checkpoint restore
Issue: - Restore operation failed with foreign key constraint violation - Error: "update or delete on table business_scopes violates foreign key constraint" - Problem: DELETE operations didn't handle foreign key dependencies correctly Solution: - Changed DELETE FROM to TRUNCATE TABLE ... CASCADE in _restore_table() - TRUNCATE with CASCADE automatically removes dependent records - Now restore operation properly handles all foreign key relationships - Data can be restored without constraint violations This ensures checkpoint restore works correctly across all related tables. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
a5d56ade26
commit
e462f609ba
File diff suppressed because one or more lines are too long
|
|
@ -386,17 +386,18 @@ def _restore_table(conn: pg.Connection, table_name: str, data: List[Dict[str, An
|
||||||
try:
|
try:
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
delete_sql = f"DELETE FROM {table_name}"
|
# Use TRUNCATE with CASCADE to handle foreign key dependencies
|
||||||
cur.execute(delete_sql)
|
# This will automatically remove dependent records
|
||||||
|
truncate_sql = f"TRUNCATE TABLE {table_name} CASCADE"
|
||||||
|
cur.execute(truncate_sql)
|
||||||
|
|
||||||
if data:
|
columns = list(data[0].keys())
|
||||||
columns = list(data[0].keys())
|
placeholders = ", ".join(["%s"] * len(columns))
|
||||||
placeholders = ", ".join(["%s"] * len(columns))
|
insert_sql = f"INSERT INTO {table_name} ({', '.join(columns)}) VALUES ({placeholders})"
|
||||||
insert_sql = f"INSERT INTO {table_name} ({', '.join(columns)}) VALUES ({placeholders})"
|
|
||||||
|
|
||||||
for row in data:
|
for row in data:
|
||||||
values = [row.get(col) for col in columns]
|
values = [row.get(col) for col in columns]
|
||||||
cur.execute(insert_sql, values)
|
cur.execute(insert_sql, values)
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return len(data)
|
return len(data)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue