Skip to content

Commit 01d5d62

Browse files
committed
Fix error when altering Part table that uses "master" keyword
1 parent 533f36f commit 01d5d62

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

datajoint/user_tables.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Hosts the table tiers, user relations should be derived from.
33
"""
44

5+
import inspect
56
from .table import Table
67
from .autopopulate import AutoPopulate
78
from .utils import from_camel_case, ClassProperty
@@ -186,3 +187,16 @@ def drop(self, force=False):
186187
super().drop()
187188
else:
188189
raise DataJointError('Cannot drop a Part directly. Delete from master instead')
190+
191+
def alter(self, prompt=True, context=None):
192+
"""
193+
Alter the table definition from self.definition
194+
"""
195+
# map "master" keyword to master table in context
196+
if context is None:
197+
frame = inspect.currentframe().f_back
198+
context = dict(frame.f_globals, **frame.f_locals)
199+
del frame
200+
if self.master:
201+
context['master'] = self.master
202+
super().alter(prompt, context)

tests/test_alter.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,26 @@ def test_alter():
3939
restored = schema.connection.query("SHOW CREATE TABLE " + Experiment.full_table_name).fetchone()[1]
4040
assert_not_equal(altered, restored)
4141
assert_equal(original, restored)
42+
43+
44+
@schema
45+
class TypeMaster(dj.Manual):
46+
definition = """
47+
master_id : int
48+
"""
49+
50+
class Type(dj.Part):
51+
definition = """
52+
-> master
53+
"""
54+
55+
definition1 = """
56+
-> TypeMaster
57+
"""
58+
59+
def test_alter_part():
60+
original = schema.connection.query("SHOW CREATE TABLE " + TypeMaster.Type.full_table_name).fetchone()[1]
61+
TypeMaster.Type.definition = Experiment.definition1
62+
TypeMaster.Type.alter(prompt=False)
63+
altered = schema.connection.query("SHOW CREATE TABLE " + TypeMaster.Type.full_table_name).fetchone()[1]
64+
assert_equal(original, altered)

0 commit comments

Comments
 (0)