I got this
_____________________________________________________________________
import psycopg2
def get_connection():
return psycopg2.connect(
host="ip of openrem",
database="openrem_prod",
user="openrem_user",
password="openrem_pass"
)
def get_wrong_institutions(cursor):
sql = """
SELECT general_study_module_attributes_id, unique_equipment_name_id
FROM remapp_generalequipmentmoduleattr
WHERE (institution_name NOT LIKE '%name1%' AND institution_name NOT LIKE '%name2%')
OR institution_name IS NULL
OR institution_name = '';
"""
cursor.execute(sql)
rows = cursor.fetchall()
study_ids = [row[0] for row in rows]
equipment_ids = [row[1] for row in rows]
return study_ids, equipment_ids
def delete_study(cursor, id):
sql = "DELETE FROM remapp_generalstudymoduleattr WHERE id = %s;"
cursor.execute(sql, (id,))
def delete_equipment(cursor, id):
sql = "DELETE FROM remapp_uniqueequipmentnames WHERE id = %s;"
cursor.execute(sql, (id,))
def main():
try:
with get_connection() as conn:
with conn.cursor() as cur:
study_ids, equipment_ids = get_wrong_institutions(cur)
for id in study_ids:
delete_study(cur, id)
for id in equipment_ids:
if id is not None:
delete_equipment(cur, id)
conn.commit()
except Exception as e:
print(f"An error occurred: {e}")
main()
___________________________________________________________________________________________________________
But i got the following error:
An error occurred: update or delete on table "remapp_generalstudymoduleattr" violates foreign key constraint "D61978a0f009ca7878ec98b861ebe601" on table "remapp_ctradiationdose"
DETAIL: Key (id)=(694946) is still referenced from table "remapp_ctradiationdose".
It looks like I have to delete lines from
remapp_ctradiationdose as well?
Op donderdag 6 juni 2024 om 00:14:10 UTC+2 schreef Ed McDonagh: