diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index 52bf770f..b048e9d0 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -97,7 +97,7 @@ class PostDetailApi(BaseApi): auth.verify_privilege(ctx.user, 'posts:delete') post = posts.get_post_by_id(post_id) snapshots.save_entity_deletion(post, ctx.user) - ctx.session.delete(post) + posts.delete(post) ctx.session.commit() tags.export_to_json() return {} diff --git a/server/szurubooru/db/post.py b/server/szurubooru/db/post.py index 38b0cbac..18dddf03 100644 --- a/server/szurubooru/db/post.py +++ b/server/szurubooru/db/post.py @@ -125,7 +125,7 @@ class Post(Base): 'PostFavorite', cascade='all, delete-orphan', lazy='joined') notes = relationship( 'PostNote', cascade='all, delete-orphan', lazy='joined') - comments = relationship('Comment') + comments = relationship('Comment', cascade='all, delete-orphan') # dynamic columns tag_count = column_property( diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index 2999dbe9..a58adbbc 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -316,3 +316,6 @@ def feature_post(post, user): post_feature.post = post post_feature.user = user db.session.add(post_feature) + +def delete(post): + db.session.delete(post) diff --git a/server/szurubooru/tests/db/test_post.py b/server/szurubooru/tests/db/test_post.py index 7a04e32c..79d10f5d 100644 --- a/server/szurubooru/tests/db/test_post.py +++ b/server/szurubooru/tests/db/test_post.py @@ -38,14 +38,15 @@ def test_saving_post(post_factory, user_factory, tag_factory): assert len(related_post1.relations) == 0 assert len(related_post2.relations) == 0 -def test_cascade_deletions(post_factory, user_factory, tag_factory): +def test_cascade_deletions(post_factory, user_factory, tag_factory, comment_factory): user = user_factory() tag1 = tag_factory() tag2 = tag_factory() related_post1 = post_factory() related_post2 = post_factory() post = post_factory() - db.session.add_all([user, tag1, tag2, post, related_post1, related_post2]) + comment = comment_factory(post=post, user=user) + db.session.add_all([user, tag1, tag2, post, related_post1, related_post2, comment]) db.session.flush() score = db.PostScore() @@ -91,6 +92,7 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory): assert db.session.query(db.PostNote).count() == 1 assert db.session.query(db.PostFeature).count() == 1 assert db.session.query(db.PostFavorite).count() == 1 + assert db.session.query(db.Comment).count() == 1 db.session.delete(post) db.session.commit() @@ -105,6 +107,7 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory): assert db.session.query(db.PostNote).count() == 0 assert db.session.query(db.PostFeature).count() == 0 assert db.session.query(db.PostFavorite).count() == 0 + assert db.session.query(db.Comment).count() == 0 def test_tracking_tag_count(post_factory, tag_factory): post = post_factory()