Skip to content

Commit 685b42c

Browse files
Merge pull request #1160 from CBroz1/1159
Cascade restriction attribute
2 parents b42b239 + 5328326 commit 685b42c

7 files changed

+57
-32
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Added - Missing tests for set_password - PR [#1106](https://github.com/datajoint/datajoint-python/pull/1106)
1212
- Changed - Returning success count after the .populate() call - PR [#1050](https://github.com/datajoint/datajoint-python/pull/1050)
1313
- Fixed - `Autopopulate.populate` excludes `reserved` jobs in addition to `ignore` and `error` jobs
14+
- Fixed - Issue [#1159]((https://github.com/datajoint/datajoint-python/pull/1159) (cascading delete) - PR [#1160](https://github.com/datajoint/datajoint-python/pull/1160)
1415

1516
### 0.14.1 -- Jun 02, 2023
1617
- Fixed - Fix altering a part table that uses the "master" keyword - PR [#991](https://github.com/datajoint/datajoint-python/pull/991)

datajoint/expression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ class U:
847847
>>> dj.U().aggr(expr, n='count(*)')
848848
849849
The following expressions both yield one element containing the number `n` of distinct values of attribute `attr` in
850-
query expressio `expr`.
850+
query expression `expr`.
851851
852852
>>> dj.U().aggr(expr, n='count(distinct attr)')
853853
>>> dj.U().aggr(dj.U('attr').aggr(expr), 'n=count(*)')

datajoint/table.py

+1
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

+22
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

+15
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

+13
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

+4-31
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)