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

Prisma Client: disconnect: true does not appear to delete the foreign key in the returned data #20491

Closed
casey-chow opened this issue Aug 2, 2023 · 8 comments · Fixed by prisma/prisma-engines#4100
Assignees
Labels
5.1.0 bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. team/client Issue for team Client. topic: disconnect topic: postgresql
Milestone

Comments

@casey-chow
Copy link

In Prisma 5.1.0 (Postgres) I noticed that my tests began failing to properly execute with:

      device = await prisma.device.update({
        where: { id: device.id },
        data: { user: { disconnect: true } },
      });

The device returned is still connected to the user after this is run.

@janpio janpio added 5.1.0 bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: disconnect topic: postgresql labels Aug 2, 2023
@janpio
Copy link
Member

janpio commented Aug 2, 2023

That is concerning @casey-chow.

Can you please turn on logging in your project when running one of these queries? (Instructions see https://pris.ly/d/logging) It would be very interesting to see the query that is being executed under the hood here. If you can downgrade to Prisma 5.0.0 as well and confirm that it correctly works there, and what query you get with that version, that would be amazing. Thank you!

To complete the information our bug issue template usually asks for:
What OS and Node version are you running? Can you maybe shared your full prisma -v output? Thanks.

@Jolg42
Copy link
Member

Jolg42 commented Aug 2, 2023

@casey-chow Along with filling more information mentioned in our bug report template, sharing your Prisma schema would also help to reproduce this.

@Jolg42
Copy link
Member

Jolg42 commented Aug 2, 2023

I could reproduce something, is that what you meant @casey-chow ?

import { PrismaClient } from "@prisma/client";

async function main() {
  const prisma = new PrismaClient();

  // Before (should be empty)
  const usersBefore = await prisma.user.findMany();
  console.log({ usersBefore });
  const devicesBefore = await prisma.device.findMany();
  console.log({ devicesBefore });

  const newDeviceWithUser = await prisma.device.create({
    data: {
      user: {
        create: {},
      },
    },
  });
  console.log({ newDeviceWithUser });

  // Disconnect!
  const disconnect = await prisma.device.update({
    where: { id: newDeviceWithUser.id },
    data: { user: { disconnect: true } },
  });
  console.log({ disconnect });

  // After
  const usersAfter = await prisma.user.findMany();
  console.log({ usersAfter });
  const devicesAfter = await prisma.device.findMany();
  console.log({ devicesAfter });

  // Cleanup
  await prisma.device.deleteMany();
  await prisma.user.deleteMany();

  prisma.$disconnect();
}

main();
// 5.0.0
// { usersBefore: [] }
// { devicesBefore: [] }
// { newDeviceWithUser: { id: 1, userId: 8 } }
// { disconnect: { id: 1, userId: null } }
// { usersAfter: [ { id: 8, name: null } ] }
// { devicesAfter: [ { id: 1, userId: null } ] }

// 5.1.0
// { usersBefore: [] }
// { devicesBefore: [] }
// { newDeviceWithUser: { id: 2, userId: 9 } }
// { disconnect: { id: 2, userId: 9 } } ---- HERE! `userId` should be null
// { usersAfter: [ { id: 9, name: null } ] }
// { devicesAfter: [ { id: 2, userId: null } ] }
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id     Int      @id @default(autoincrement())
  name   String?
  Device Device[]
}

model Device {
  id     Int   @id @default(autoincrement())
  user   User? @relation(fields: [userId], references: [id])
  userId Int?
}

@Jolg42
Copy link
Member

Jolg42 commented Aug 2, 2023

So it looks like the data returned by a disconnect query is wrong here, though the data is actually right in the database.

await prisma.device.update({
    where: { id: newDeviceWithUser.id },
    data: { user: { disconnect: true } },
  });

returns { id: 1, userId: null } in 5.0.0
but returns { id: 2, userId: 9 } in 5.1.0

@Jolg42 Jolg42 added bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. labels Aug 2, 2023
@Jolg42 Jolg42 added this to the 5.2.0 milestone Aug 2, 2023
@Jolg42
Copy link
Member

Jolg42 commented Aug 2, 2023

Note that we are looking at this and might issue a patch release once we have a fix, we'll keep you updated, thanks for the report 🙌🏼

@Jolg42 Jolg42 changed the title disconnect: true does not delete foreign keys Prisma Client: disconnect: true does not appear to delete the foreign key in the returned data Aug 2, 2023
@Jolg42
Copy link
Member

Jolg42 commented Aug 2, 2023

Note: we got a fix, more info coming soon.

@casey-chow
Copy link
Author

What a flurry to wake up to! Apologies for not following bug template, I figured there should be one but for whatever reason it didn't ever pop up for me in GitHub, and it was near the end of the day and wanted to get this information over to your team quickly.

Yes, I was able to verify it worked in 5.0.0 as well. Thanks for getting this fix in so quickly, look forward to seeing the patch release!

@SevInf
Copy link
Contributor

SevInf commented Aug 3, 2023

Fix released in 5.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.1.0 bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. team/client Issue for team Client. topic: disconnect topic: postgresql
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants