Skip to content

Commit

Permalink
Merge pull request #2 from dimitri-yatsenko/populate-error
Browse files Browse the repository at this point in the history
fix datajoint#902: re-load dependencies after table declaration.
  • Loading branch information
guzman-raphael authored May 20, 2021
2 parents ec7b2e0 + eb86b41 commit d5157d0
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions datajoint/autopopulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def _rename_attributes(table, props):
if self._key_source is None:
parents = self.target.parents(primary=True, as_objects=True, foreign_key_info=True)
if not parents:
raise DataJointError(
'A relation must have primary dependencies for auto-populate to work')
raise DataJointError('A table must have dependencies '
'from its primary key for auto-populate to work')
self._key_source = _rename_attributes(*parents[0])
for q in parents[1:]:
self._key_source *= _rename_attributes(*q)
Expand Down
2 changes: 1 addition & 1 deletion datajoint/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def ordered_dir(class_):
"""
List (most) attributes of the class including inherited ones, similar to `dir` build-in function,
but respects order of attribute declaration as much as possible.
This becomes unnecessary in Python 3.6+ as dicts became ordered.
:param class_: class to list members for
:return: a list of attributes declared in class_ and its superclasses
"""
Expand Down Expand Up @@ -186,6 +185,7 @@ def _decorate_table(self, table_class, context, assert_declared=False):
if not self.create_tables or assert_declared:
raise DataJointError('Table `%s` not declared' % instance.table_name)
instance.declare(context)
self.connection.dependencies.clear()
is_declared = is_declared or instance.is_declared

# add table definition to the doc string
Expand Down
2 changes: 2 additions & 0 deletions tests/test_autopopulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Image(dj.Imported):

def make(self, key):
self.insert1(dict(key, image_data=dict()))

Image.populate()

@schema
Expand All @@ -98,4 +99,5 @@ class Crop(dj.Computed):

def make(self, key):
self.insert1(dict(key, crop_image=dict()))

Crop.populate()
1 change: 1 addition & 0 deletions tests/test_erd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_decorator():
@staticmethod
def test_dependencies():
deps = schema.connection.dependencies
deps.load()
assert_true(all(cls.full_table_name in deps for cls in (A, B, B.C, D, E, E.F, L)))
assert_true(set(A().children()) == set([B.full_table_name, D.full_table_name]))
assert_true(set(D().parents(primary=True)) == set([A.full_table_name]))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_schema_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from nose.tools import assert_true


schema = dj.Schema(PREFIX + '_keywords', locals(), connection=dj.conn(**CONN_INFO))
schema = dj.Schema(PREFIX + '_keywords', connection=dj.conn(**CONN_INFO))


@schema
Expand Down

0 comments on commit d5157d0

Please sign in to comment.