datajoint-python icon indicating copy to clipboard operation
datajoint-python copied to clipboard

Cannot use alter on a part table

Open dpeg22 opened this issue 2 years ago • 2 comments

Bug Report

Description

When trying to use the alter method on a parts table that is dependent on it's master table, it errors out with DatajointError: Foreign key reference master could not be resolved.

Reproducibility

Include:

  • Linux
  • Python Version 3.8.13
  • MySQL Version 5.7.37-0ubuntu0.18.04.1 (Ubuntu)
  • MySQL Deployment Strategy local-docker
  • DataJoint Version 0.13.4
  • Minimum number of steps to reliably reproduce the issue:
  1. Create a master and part table making sure the master table is a primary attribute of the part table
  2. Add a secondary attribute to the part table
  3. Run PartTableName.alter().
  • Complete error stack as a result of evaluating the above steps:
---------------------------------------------------------------------------
DataJointError                            Traceback (most recent call last)
Input In [41], in <module>
----> 1 ss.CuratedSpikeSorting.Unit().alter()

File ~/anaconda3/envs/nwb_datajoint_sv_si/lib/python3.8/site-packages/datajoint/table.py:104, in Table.alter(self, prompt, context)
    102     del frame
    103 old_definition = self.describe(context=context, printout=False)
--> 104 sql, external_stores = alter(self.definition, old_definition, context)
    105 if not sql:
    106     if prompt:

File ~/anaconda3/envs/nwb_datajoint_sv_si/lib/python3.8/site-packages/datajoint/declare.py:360, in alter(definition, old_definition, context)
    353 def alter(definition, old_definition, context):
    354     """
    355     :param definition: new table definition
    356     :param old_definition: current table definition
    357     :param context: the context in which to evaluate foreign key definitions
    358     :return: string SQL ALTER command, list of new stores used for external storage
    359     """
--> 360     table_comment, primary_key, attribute_sql, foreign_key_sql, index_sql, external_stores = prepare_declare(
    361         definition, context)
    362     table_comment_, primary_key_, attribute_sql_, foreign_key_sql_, index_sql_, external_stores_ = prepare_declare(
    363         old_definition, context)
    365     # analyze differences between declarations

File ~/anaconda3/envs/nwb_datajoint_sv_si/lib/python3.8/site-packages/datajoint/declare.py:248, in prepare_declare(definition, context)
    246     in_key = False  # start parsing dependent attributes
    247 elif is_foreign_key(line):
--> 248     compile_foreign_key(line, context, attributes,
    249                         primary_key if in_key else None,
    250                         attribute_sql, foreign_key_sql, index_sql)
    251 elif re.match(r'^(unique\s+)?index[^:]*$', line, re.I):   # index
    252     compile_index(line, index_sql)

File ~/anaconda3/envs/nwb_datajoint_sv_si/lib/python3.8/site-packages/datajoint/declare.py:143, in compile_foreign_key(line, context, attributes, primary_key, attr_sql, foreign_key_sql, index_sql)
    141     ref = eval(result.ref_table, context)
    142 except NameError if obsolete else Exception:
--> 143     raise DataJointError('Foreign key reference %s could not be resolved' % result.ref_table)
    145 options = [opt.upper() for opt in result.options]
    146 for opt in options:  # check for invalid options

DataJointError: Foreign key reference CuratedSpikeSorting could not be resolved

Expected Behavior

Expected a prompt asking if I wanted to alter the table, which would then add the secondary attribute and populate the table with the default value for that attribute.

Additional Research and Context

Messaged the Datajoint slack channel, which suggested filing an issue

dpeg22 avatar Apr 29 '22 22:04 dpeg22

Reviewing outstanding PRs - Would this be fixed by PR #991, that gives the master tag better context?

CBroz1 avatar May 06 '22 19:05 CBroz1

I would like to work on this issue. Reply ASAP.

Prayag-raj-sharma avatar Sep 14 '22 13:09 Prayag-raj-sharma

Duplicate #936

dimitri-yatsenko avatar Oct 07 '22 20:10 dimitri-yatsenko