Skip to content

Commit e567ca1

Browse files
Make error message parsing more explicit and simplify logic.
1 parent 1ab733c commit e567ca1

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

datajoint/table.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -348,37 +348,31 @@ def _delete_cascade(self):
348348
try:
349349
delete_count += self.delete_quick(get_count=True)
350350
except IntegrityError as error:
351-
try:
352-
# try to parse error for child info
353-
match = foreign_key_full_error_regexp.match(error.args[0]).groupdict()
354-
if "`.`" not in match['child']: # if schema name missing, use self
355-
match['child'] = '{}.{}'.format(self.full_table_name.split(".")[0],
356-
match['child'])
351+
match = (foreign_key_full_error_regexp.match(error.args[0]) or
352+
foreign_key_partial_error_regexp.match(error.args[0])).groupdict()
353+
if "`.`" not in match['child']: # if schema name missing, use self
354+
match['child'] = '{}.{}'.format(self.full_table_name.split(".")[0],
355+
match['child'])
356+
if 'pk_attrs' in match: # fullly matched, adjusting the keys
357357
match['fk_attrs'] = [k.strip('`') for k in match['fk_attrs'].split(',')]
358358
match['pk_attrs'] = [k.strip('`') for k in match['pk_attrs'].split(',')]
359-
except AttributeError:
360-
# additional query required due to truncated error, trying partial regex
361-
match = foreign_key_partial_error_regexp.match(error.args[0]).groupdict()
362-
if "`.`" not in match['child']: # if schema name missing, use self
363-
match['child'] = '{}.{}'.format(self.full_table_name.split(".")[0],
364-
match['child'])
359+
else: # only partially matched, querying with constraint to determine keys
365360
match['fk_attrs'], match['parent'], match['pk_attrs'] = list(map(
366-
list, zip(
367-
*self.connection.query(
368-
"""
369-
SELECT
370-
COLUMN_NAME as fk_attrs,
371-
CONCAT('`', REFERENCED_TABLE_SCHEMA, '`.`',
372-
REFERENCED_TABLE_NAME, '`') as parent,
373-
REFERENCED_COLUMN_NAME as pk_attrs
374-
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
375-
WHERE
376-
CONSTRAINT_NAME = %s AND TABLE_SCHEMA = %s
377-
AND TABLE_NAME = %s;
378-
""",
379-
args=(match['name'].strip('`'),
380-
*[_.strip('`') for _ in match['child'].split('`.`')])
381-
).fetchall())))
361+
list, zip(*self.connection.query(
362+
"""
363+
SELECT
364+
COLUMN_NAME as fk_attrs,
365+
CONCAT('`', REFERENCED_TABLE_SCHEMA, '`.`',
366+
REFERENCED_TABLE_NAME, '`') as parent,
367+
REFERENCED_COLUMN_NAME as pk_attrs
368+
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
369+
WHERE
370+
CONSTRAINT_NAME = %s AND TABLE_SCHEMA = %s
371+
AND TABLE_NAME = %s;
372+
""",
373+
args=(match['name'].strip('`'),
374+
*[_.strip('`') for _ in match['child'].split('`.`')])
375+
).fetchall())))
382376
match['parent'] = match['parent'][0]
383377
# restrict child by self if
384378
# 1. if self's restriction attributes are not in child's primary key

0 commit comments

Comments
 (0)