Skip to content

Commit

Permalink
First pass implementation of cascading delete datajoint#15
Browse files Browse the repository at this point in the history
  • Loading branch information
eywalker committed Jul 17, 2015
1 parent cb90b26 commit 81a6878
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions datajoint/relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Mapping
from collections import defaultdict
import numpy as np
import logging
import abc
Expand Down Expand Up @@ -82,10 +83,16 @@ def children(self):

@property
def references(self):
"""
:return: list of tables that this tables refers to
"""
return self.connection.erm.references[self.full_table_name]

@property
def referenced(self):
"""
:return: list of tables for which this table is referenced by
"""
return self.connection.erm.referenced[self.full_table_name]

@property
Expand Down Expand Up @@ -187,8 +194,22 @@ def delete(self):
User is prompted for confirmation if config['safemode']
"""
relations = self.descendants
if self.restrictions and len(relations)>1:
raise NotImplementedError('Restricted cascading deletes are not yet implemented')
#if self.restrictions and len(relations)>1:
# raise NotImplementedError('Restricted cascading deletes are not yet implemented')
restrict_by_me = defaultdict(lambda: False)
rel_by_name = {r.full_table_name:r for r in relations}
for r in relations:
for ref in r.references:
restrict_by_me[ref] = True

if self.restrictions is not None:
restrict_by_me[self.full_table_name] = True
rel_by_name[self.full_table_name]._restrict(self.restrictions)

for r in relations:
for dep in (r.children + r.references):
rel_by_name[dep]._restrict(r.project() if restrict_by_me[r.full_table_name] else r.restrictions)

do_delete = True
if config['safemode']:
do_delete = False
Expand Down

0 comments on commit 81a6878

Please sign in to comment.