Add backref cascade and make changes to the spot where we delete so it loads objects into memory and then uses the SQLAlchemy plumbing to make sure that the db object gets cleaned up.

This commit is contained in:
Kelly McDonald 2021-08-18 08:25:17 -04:00
parent 6ce560cc2d
commit b4ecb0f97a
2 changed files with 6 additions and 3 deletions

View File

@ -138,7 +138,9 @@ def delete_workflow_specification(spec_id):
# Delete all events and workflow models related to this specification # Delete all events and workflow models related to this specification
for workflow in session.query(WorkflowModel).filter_by(workflow_spec_id=spec_id): for workflow in session.query(WorkflowModel).filter_by(workflow_spec_id=spec_id):
StudyService.delete_workflow(workflow.id) StudyService.delete_workflow(workflow.id)
session.query(WorkflowSpecModel).filter_by(id=spec_id).delete() # the cascade feature doesn't work if
deleteSpec = session.query(WorkflowSpecModel).filter_by(id=spec_id).first()
session.delete(deleteSpec)
session.commit() session.commit()

View File

@ -4,6 +4,7 @@ import marshmallow
from marshmallow import EXCLUDE,fields from marshmallow import EXCLUDE,fields
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
from sqlalchemy import func from sqlalchemy import func
from sqlalchemy.orm import backref
from crc import db from crc import db
from crc.models.file import FileModel, FileDataModel from crc.models.file import FileModel, FileDataModel
@ -46,9 +47,9 @@ class WorkflowLibraryModel(db.Model):
library_spec_id = db.Column(db.String, db.ForeignKey('workflow_spec.id'), nullable=True) library_spec_id = db.Column(db.String, db.ForeignKey('workflow_spec.id'), nullable=True)
parent = db.relationship(WorkflowSpecModel, parent = db.relationship(WorkflowSpecModel,
primaryjoin=workflow_spec_id==WorkflowSpecModel.id, primaryjoin=workflow_spec_id==WorkflowSpecModel.id,
backref='libraries') backref=backref('libraries',cascade='all, delete'))
library = db.relationship(WorkflowSpecModel,primaryjoin=library_spec_id==WorkflowSpecModel.id, library = db.relationship(WorkflowSpecModel,primaryjoin=library_spec_id==WorkflowSpecModel.id,
backref='parents') backref=backref('parents',cascade='all, delete'))
class WorkflowSpecModelSchema(SQLAlchemyAutoSchema): class WorkflowSpecModelSchema(SQLAlchemyAutoSchema):