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

populate() throws an error if schema.list_tables() is called before a computed table's definition and not recalled afterwards

Open A-Baji opened this issue 3 years ago • 0 comments

Bug Report

Description

Calling schema.list_tables() at any point before a computed table's definition, and then calling the computed table's populate() method without calling schema.list_tables() again beforehand throws a DataJoint error.

Reproducibility

  • Linux
  • Python 3
  • DataJoint 0.13.2
  • Steps:
    1. Create an manual or lookup table
    2. Call schema.list_tables()
    3. Create a computed table using the previous table as a foreign key
    4. Call the computed table's populate()
print(schema.list_tables()) 

@schema
class Subject(dj.Lookup):
    definition = """
    subject_id: int
    """
    contents = [dict(subject_id=1)] 

@schema
class Session(dj.Computed):
    definition = """
    -> Subject
    time: varchar(30)
    """
    
    def make(self, key):
        new_entry = dict(**key, time=datetime.now())
        self.insert1(new_entry)
    
Session.populate()
  • Complete error stack as a result of evaluating the above steps:
Traceback (most recent call last):
  File "/home/.anaconda/test.py", line 29, in <module>
    Session.populate() # invokes Session's make(), passing Subject's primary key
  File "/opt/conda/lib/python3.9/site-packages/datajoint/autopopulate.py", line 128, in populate
    keys = (self._jobs_to_do(restrictions) - self.target).fetch("KEY", limit=limit)
  File "/opt/conda/lib/python3.9/site-packages/datajoint/autopopulate.py", line 80, in _jobs_to_do
    todo = self.key_source
  File "/opt/conda/lib/python3.9/site-packages/datajoint/autopopulate.py", line 42, in key_source
    raise DataJointError(
datajoint.errors.DataJointError: A relation must have primary dependencies for auto-populate to work

Expected Behavior

The computed table's populate() should successfully run its make() function, passing the foreign key's values as the key parameter.

Screenshots

[doesn't work] Calling schema.list_tables only before the computed table's definition: image

[works] Calling schema.list_tables only after the computed table's definition: image

[works] Calling schema.list_tables before and after the computed table's definition: image

A-Baji avatar Jan 28 '22 23:01 A-Baji