Skip to content

Commit 862a1f8

Browse files
committed
Cascade restriction attribute
1 parent b42b239 commit 862a1f8

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

datajoint/table.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ def cascade(table):
559559
and match["fk_attrs"] == match["pk_attrs"]
560560
):
561561
child._restriction = table._restriction
562+
child._restriction_attributes = table.restriction_attributes
562563
elif match["fk_attrs"] != match["pk_attrs"]:
563564
child &= table.proj(
564565
**dict(zip(match["fk_attrs"], match["pk_attrs"]))

tests/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ def schema_any(connection_test, prefix):
285285
schema_any(schema.ThingA)
286286
schema_any(schema.ThingB)
287287
schema_any(schema.ThingC)
288+
schema_any(schema.ThingD)
289+
schema_any(schema.ThingE)
288290
schema_any(schema.Parent)
289291
schema_any(schema.Child)
290292
schema_any(schema.ComplexParent)
@@ -303,6 +305,26 @@ def schema_any(connection_test, prefix):
303305
schema_any.drop()
304306

305307

308+
@pytest.fixture
309+
def thing_tables(schema_any):
310+
a = schema.ThingA()
311+
b = schema.ThingB()
312+
c = schema.ThingC()
313+
d = schema.ThingD()
314+
e = schema.ThingE()
315+
316+
# clear previous contents if any.
317+
c.delete_quick()
318+
b.delete_quick()
319+
a.delete_quick()
320+
321+
a.insert(dict(a=a) for a in range(7))
322+
b.insert1(dict(b1=1, b2=1, b3=100))
323+
b.insert1(dict(b1=1, b2=2, b3=100))
324+
325+
yield a, b, c, d, e
326+
327+
306328
@pytest.fixture
307329
def schema_simp(connection_test, prefix):
308330
schema = dj.Schema(

tests/schema.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,21 @@ class ThingC(dj.Manual):
345345
"""
346346

347347

348+
# Additional tables for #1159
349+
class ThingD(dj.Manual):
350+
definition = """
351+
d: int
352+
---
353+
-> ThingC
354+
"""
355+
356+
357+
class ThingE(dj.Manual):
358+
definition = """
359+
-> ThingD
360+
"""
361+
362+
348363
class Parent(dj.Lookup):
349364
definition = """
350365
parent_id: int

tests/test_cascading_delete.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,16 @@ def test_drop_part(schema_simp_pop):
121121
"""test issue #374"""
122122
with pytest.raises(dj.DataJointError):
123123
Website().drop()
124+
125+
126+
def test_delete_1159(thing_tables):
127+
tbl_a, tbl_c, tbl_c, tbl_d, tbl_e = thing_tables
128+
129+
tbl_c.insert([dict(a=i) for i in range(6)])
130+
tbl_d.insert([dict(a=i, d=i) for i in range(5)])
131+
tbl_e.insert([dict(d=i) for i in range(4)])
132+
133+
(tbl_a & "a=3").delete()
134+
135+
assert len(tbl_a) == 6, "Failed to cascade restriction attributes"
136+
assert len(tbl_e) == 3, "Failed to cascade restriction attributes"

tests/test_dependencies.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import datajoint as dj
21
from datajoint import errors
32
from pytest import raises
43
from datajoint.dependencies import unite_master_parts
5-
from .schema import *
64

75

86
def test_unite_master_parts():
@@ -50,22 +48,10 @@ def test_unite_master_parts():
5048
]
5149

5250

53-
def test_nullable_dependency(schema_any):
51+
def test_nullable_dependency(thing_tables):
5452
"""test nullable unique foreign key"""
5553
# Thing C has a nullable dependency on B whose primary key is composite
56-
a = ThingA()
57-
b = ThingB()
58-
c = ThingC()
59-
60-
# clear previous contents if any.
61-
c.delete_quick()
62-
b.delete_quick()
63-
a.delete_quick()
64-
65-
a.insert(dict(a=a) for a in range(7))
66-
67-
b.insert1(dict(b1=1, b2=1, b3=100))
68-
b.insert1(dict(b1=1, b2=2, b3=100))
54+
_, _, c, _, _ = thing_tables
6955

7056
# missing foreign key attributes = ok
7157
c.insert1(dict(a=0))
@@ -79,23 +65,10 @@ def test_nullable_dependency(schema_any):
7965
assert len(c) == len(c.fetch()) == 5
8066

8167

82-
def test_unique_dependency(schema_any):
68+
def test_unique_dependency(thing_tables):
8369
"""test nullable unique foreign key"""
84-
8570
# Thing C has a nullable dependency on B whose primary key is composite
86-
a = ThingA()
87-
b = ThingB()
88-
c = ThingC()
89-
90-
# clear previous contents if any.
91-
c.delete_quick()
92-
b.delete_quick()
93-
a.delete_quick()
94-
95-
a.insert(dict(a=a) for a in range(7))
96-
97-
b.insert1(dict(b1=1, b2=1, b3=100))
98-
b.insert1(dict(b1=1, b2=2, b3=100))
71+
_, _, c, _, _ = thing_tables
9972

10073
c.insert1(dict(a=0, b1=1, b2=1))
10174
# duplicate foreign key attributes = not ok

0 commit comments

Comments
 (0)