From 0a8fb1cde4ceb33ecdbf4a2dc2eb1fc0e42e9497 Mon Sep 17 00:00:00 2001 From: Mike Benowitz Date: Mon, 5 Aug 2019 14:41:27 -0400 Subject: [PATCH] Fix issues with delete-cascade The important tables `xml` and `registration` were not properly set for their `CASCADE` behavior, in addiiton `XML` needed to have the `single_parent` option enabled to allow for cascading-deletes (since otherwise a single entry could be referenced by an entry and a error. --- model/cce.py | 6 +++++- model/xml.py | 15 +++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/model/cce.py b/model/cce.py index 1bd3397..639087f 100644 --- a/model/cce.py +++ b/model/cce.py @@ -39,7 +39,11 @@ class CCE(Core, Base): volume_id = Column(Integer, ForeignKey('volume.id')) - registrations = relationship('Registration', backref='cce') + registrations = relationship( + 'Registration', + backref='cce', + cascade='all, delete-orphan' + ) lccns = relationship('LCCN', backref='cce', cascade='all, delete-orphan') authors = relationship('Author', backref='cce', cascade='all, delete-orphan') diff --git a/model/xml.py b/model/xml.py index 8697ac7..fc2a44b 100644 --- a/model/xml.py +++ b/model/xml.py @@ -14,9 +14,11 @@ from sqlalchemy import ( from model.core import Base, Core + @compiles(String, 'postgresql') def compile_xml(type_, compiler, **kw): - return "XML" + return 'XML' + ENTRY_XML = Table( 'entry_xml', @@ -32,19 +34,24 @@ ERROR_XML = Table( Column('xml_id', Integer, ForeignKey('xml.id'), index=True) ) + class XML(Core, Base): __tablename__ = 'xml' id = Column(Integer, primary_key=True) xml_source = Column(String) - + entry = relationship( 'CCE', secondary=ENTRY_XML, - backref='xml_sources' + backref='xml_sources', + single_parent=True, + cascade='all, delete-orphan' ) error_entry = relationship( 'ErrorCCE', secondary=ERROR_XML, - backref='xml_sources' + backref='xml_sources', + single_parent=True, + cascade='all, delete-orphan' )