Merge pull request #991 from nagisa77/codex/fix-foreign-key-constraint-error-on-deletepost-mrgsx4

Delete post change logs before removing posts
This commit is contained in:
Tim
2025-09-17 13:31:31 +08:00
committed by GitHub
4 changed files with 8 additions and 0 deletions

View File

@@ -8,4 +8,6 @@ import java.util.List;
public interface PostChangeLogRepository extends JpaRepository<PostChangeLog, Long> {
List<PostChangeLog> findByPostOrderByCreatedAtAsc(Post post);
void deleteByPost(Post post);
}

View File

@@ -109,6 +109,10 @@ public class PostChangeLogService {
logRepository.save(log);
}
public void deleteLogsForPost(Post post) {
logRepository.deleteByPost(post);
}
public List<PostChangeLog> listLogs(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));

View File

@@ -888,6 +888,7 @@ public class PostService {
}
}
String title = post.getTitle();
postChangeLogService.deleteLogsForPost(post);
postRepository.delete(post);
if (adminDeleting) {
notificationService.createNotification(author, NotificationType.POST_DELETED,

View File

@@ -71,6 +71,7 @@ class PostServiceTest {
verify(postReadService).deleteByPost(post);
verify(postRepo).delete(post);
verify(postChangeLogService).deleteLogsForPost(post);
}
@Test