Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade restriction attribute #1160

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Added - Missing tests for set_password - PR [#1106](https://github.com/datajoint/datajoint-python/pull/1106)
- Changed - Returning success count after the .populate() call - PR [#1050](https://github.com/datajoint/datajoint-python/pull/1050)
- Fixed - `Autopopulate.populate` excludes `reserved` jobs in addition to `ignore` and `error` jobs
- Fixed - Issue [#1159]((https://github.com/datajoint/datajoint-python/pull/1159) (cascading delete) - PR [#1160](https://github.com/datajoint/datajoint-python/pull/1160)

### 0.14.1 -- Jun 02, 2023
- Fixed - Fix altering a part table that uses the "master" keyword - PR [#991](https://github.com/datajoint/datajoint-python/pull/991)
Expand Down
2 changes: 1 addition & 1 deletion datajoint/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ class U:
>>> dj.U().aggr(expr, n='count(*)')

The following expressions both yield one element containing the number `n` of distinct values of attribute `attr` in
query expressio `expr`.
query expression `expr`.

>>> dj.U().aggr(expr, n='count(distinct attr)')
>>> dj.U().aggr(dj.U('attr').aggr(expr), 'n=count(*)')
Expand Down
1 change: 1 addition & 0 deletions datajoint/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ def cascade(table):
and match["fk_attrs"] == match["pk_attrs"]
):
child._restriction = table._restriction
child._restriction_attributes = table.restriction_attributes
elif match["fk_attrs"] != match["pk_attrs"]:
child &= table.proj(
**dict(zip(match["fk_attrs"], match["pk_attrs"]))
Expand Down
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def schema_any(connection_test, prefix):
schema_any(schema.ThingA)
schema_any(schema.ThingB)
schema_any(schema.ThingC)
schema_any(schema.ThingD)
schema_any(schema.ThingE)
schema_any(schema.Parent)
schema_any(schema.Child)
schema_any(schema.ComplexParent)
Expand All @@ -303,6 +305,26 @@ def schema_any(connection_test, prefix):
schema_any.drop()


@pytest.fixture
def thing_tables(schema_any):
a = schema.ThingA()
b = schema.ThingB()
c = schema.ThingC()
d = schema.ThingD()
e = schema.ThingE()

# clear previous contents if any.
c.delete_quick()
b.delete_quick()
a.delete_quick()

a.insert(dict(a=a) for a in range(7))
b.insert1(dict(b1=1, b2=1, b3=100))
b.insert1(dict(b1=1, b2=2, b3=100))

yield a, b, c, d, e


@pytest.fixture
def schema_simp(connection_test, prefix):
schema = dj.Schema(
Expand Down
15 changes: 15 additions & 0 deletions tests/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ class ThingC(dj.Manual):
"""


# Additional tables for #1159
class ThingD(dj.Manual):
definition = """
d: int
---
-> ThingC
"""


class ThingE(dj.Manual):
definition = """
-> ThingD
"""


class Parent(dj.Lookup):
definition = """
parent_id: int
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cascading_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,16 @@ def test_drop_part(schema_simp_pop):
"""test issue #374"""
with pytest.raises(dj.DataJointError):
Website().drop()


def test_delete_1159(thing_tables):
tbl_a, tbl_c, tbl_c, tbl_d, tbl_e = thing_tables

tbl_c.insert([dict(a=i) for i in range(6)])
tbl_d.insert([dict(a=i, d=i) for i in range(5)])
tbl_e.insert([dict(d=i) for i in range(4)])

(tbl_a & "a=3").delete()

assert len(tbl_a) == 6, "Failed to cascade restriction attributes"
assert len(tbl_e) == 3, "Failed to cascade restriction attributes"
35 changes: 4 additions & 31 deletions tests/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import datajoint as dj
from datajoint import errors
from pytest import raises
from datajoint.dependencies import unite_master_parts
from .schema import *


def test_unite_master_parts():
Expand Down Expand Up @@ -50,22 +48,10 @@ def test_unite_master_parts():
]


def test_nullable_dependency(schema_any):
def test_nullable_dependency(thing_tables):
"""test nullable unique foreign key"""
# Thing C has a nullable dependency on B whose primary key is composite
a = ThingA()
b = ThingB()
c = ThingC()

# clear previous contents if any.
c.delete_quick()
b.delete_quick()
a.delete_quick()

a.insert(dict(a=a) for a in range(7))

b.insert1(dict(b1=1, b2=1, b3=100))
b.insert1(dict(b1=1, b2=2, b3=100))
_, _, c, _, _ = thing_tables

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


def test_unique_dependency(schema_any):
def test_unique_dependency(thing_tables):
"""test nullable unique foreign key"""

# Thing C has a nullable dependency on B whose primary key is composite
a = ThingA()
b = ThingB()
c = ThingC()

# clear previous contents if any.
c.delete_quick()
b.delete_quick()
a.delete_quick()

a.insert(dict(a=a) for a in range(7))

b.insert1(dict(b1=1, b2=1, b3=100))
b.insert1(dict(b1=1, b2=2, b3=100))
_, _, c, _, _ = thing_tables

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