How to cascade delete records?¶
This example is using Demo Albums and Artists tables.
For any Albums record, it is not possible to delete the record if
the lookup Artist record exists in the Artists table. And vice versa.
However, if Soft Delete is used,
we can use the below Server Module code to change the Deleted flag
to True.
For tables with the “Foreign Key” constraint, only the Soft Delete is needed to achieve the same result.
The records will not be physically deleted in this case.
For table with no “Foreign Key” constraint and no Soft Delete, the records will be deleted permanently.
The below code is within the Albums Server Module:
def cascade_artist_children(delta=None, connection=None):
artists = task.artists.copy(handlers=False)
artists.set_where(id=delta.artist.value)
artists.open()
for i in range(artists.record_count()):
artists.rec_no = i
artists.delete()
artists.apply()
print("Delete applied successfully!")
def on_before_apply_record(item, delta, params, connection):
if delta and hasattr(delta, 'rec_deleted') and delta.rec_deleted():
cascade_artist_children(delta, connection)
delta._lookup_refs = {}
Similar code for Artists table:
def cascade_albums_children(delta=None, connection=None):
albums = task.albums.copy(handlers=False)
albums.set_where(artist=delta.id.value)
albums.open()
for i in range(albums.record_count()):
albums.rec_no = i
albums.delete()
albums.apply()
print("Delete applied successfully!")
def on_before_apply_record(item, delta, params, connection):
if delta and hasattr(delta, 'rec_deleted') and delta.rec_deleted():
cascade_albums_children(delta, connection)
delta._lookup_refs = {}
The JS code to alert the user with delete status:
function on_before_delete(item) {
item.alert_success('Successful delete!');
return false;
}