Fix foreign key constraint error when deleting orphaned jobs
Moved mail deletion to after job deletion to avoid foreign key constraint violations. The job_runs have a foreign key to mail_messages, so jobs (and their cascaded runs) must be deleted first before the mails can be deleted. Correct order: 1. Clean up auxiliary tables (ticket_job_runs, remark_job_runs, etc) 2. Delete jobs (cascades to runs via ORM) 3. Delete mails (no more foreign key references) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
82fff08ebb
commit
f332e61288
@ -197,11 +197,6 @@ def settings_jobs_delete_orphaned():
|
|||||||
if run.mail_message_id:
|
if run.mail_message_id:
|
||||||
mail_message_ids.append(run.mail_message_id)
|
mail_message_ids.append(run.mail_message_id)
|
||||||
|
|
||||||
# Delete related mails permanently (customer is gone)
|
|
||||||
if mail_message_ids:
|
|
||||||
mail_count = len(mail_message_ids)
|
|
||||||
MailMessage.query.filter(MailMessage.id.in_(mail_message_ids)).delete(synchronize_session=False)
|
|
||||||
|
|
||||||
# Helper function for safe SQL execution
|
# Helper function for safe SQL execution
|
||||||
def _safe_execute(stmt, params):
|
def _safe_execute(stmt, params):
|
||||||
try:
|
try:
|
||||||
@ -252,6 +247,12 @@ def settings_jobs_delete_orphaned():
|
|||||||
for job in orphaned_jobs:
|
for job in orphaned_jobs:
|
||||||
db.session.delete(job)
|
db.session.delete(job)
|
||||||
|
|
||||||
|
# Now delete related mails permanently (customer is gone)
|
||||||
|
# This must happen AFTER deleting jobs/runs to avoid foreign key constraint violations
|
||||||
|
if mail_message_ids:
|
||||||
|
mail_count = len(mail_message_ids)
|
||||||
|
MailMessage.query.filter(MailMessage.id.in_(mail_message_ids)).delete(synchronize_session=False)
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
flash(
|
flash(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user