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

Bug: Soft Delete Bypasses onDelete: Restrict Constraint #35

Open
shahadat3669 opened this issue Mar 3, 2025 · 0 comments
Open

Bug: Soft Delete Bypasses onDelete: Restrict Constraint #35

shahadat3669 opened this issue Mar 3, 2025 · 0 comments

Comments

@shahadat3669
Copy link

Bug: Soft Delete Bypasses onDelete: Restrict Constraint

Description

The Prisma Extension for soft delete is deleting related records even when the schema explicitly sets onDelete: Restrict. According to Prisma's behavior, the onDelete: Restrict rule should prevent a User from being deleted if they have related Post records. However, using this extension, the user is still soft deleted, even though they have related posts.

Steps to Reproduce

  1. Define the following Prisma schema:

    model User {
      id   Int     @id @default(autoincrement())
      name String?
    
      // relation
      Post Post[]
    
      // audit fields
      createdAt DateTime  @default(now())
      deletedAt DateTime?
    }
    
    model Post {
      id       Int     @id @default(autoincrement())
      title    String
      content  String?
      authorId Int?
    
      // relation
      author User? @relation(fields: [authorId], references: [id], onDelete: Restrict)
    
      // audit fields
      createdAt DateTime  @default(now())
      deletedAt DateTime?
    }
  2. Create a user and a related post:

    const user = await prisma.user.create({
      data: {
        name: "John Doe",
        Post: {
          create: {
            title: "Sample Post",
            content: "This is a test post",
          },
        },
      },
    });
  3. Attempt to soft delete the user:

    await prisma.user.delete({
      where: { id: user.id },
    });

Expected Behavior

The delete operation should be blocked due to the onDelete: Restrict rule.

Actual Behavior

The user is deleted successfully, even though they have a related Post record. This violates the expected behavior of onDelete: Restrict.

Possible Fix

Ensure that the extension respects onDelete: Restrict and prevents deletion when related records exist.

Additional Context

If the extension is overriding Prisma's default behavior, it should at least provide an option to enforce onDelete: Restrict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant