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

Nested update with updateMany does not update the updatedAt timestamps for related records properly #8265

Closed
mick703 opened this issue Jul 14, 2021 · 2 comments
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client.
Milestone

Comments

@mick703
Copy link

mick703 commented Jul 14, 2021

Bug description

Hi, we found an issue with the updated_at timestamp not getting updated when we use updateMany in a nested update. When we update order with its children order lines by using the nested update, the timestamps updated_at on the order lines do not get updated automatically with @updatedAt specified in the schema. I do not see an issue reported in here. Did I miss anything or this is a known issue?

How to reproduce

  • Create an order with an order line
  • Use prisma.order.update to update the order with nested update updateMany to update its order line records, eg, update the sku to have a new value.
  • Retrive the order line record by prisma findFirst.

Here is the test cases,

  describe('prisma client', () => {
    it('prisma should update updated_at timestamp properly with updateMany', async () => {
      const order = Order.create(allOrderProps()).getValue();
      await repo.save(order);
      const prismaOrderCreated = await prisma.order.findFirst({
        where: { id: order.id.toValue() },
        include: { order_lines: true }
      });
      expect(prismaOrderCreated.order_lines[0].updated_at).toBeDefined();

      //Use Prisma query to update the order line
      const orderLineId = prismaOrderCreated.order_lines[0].id;
      await prisma.orderLine.updateMany({
        where: { id: orderLineId },
        data: { external_id: 'updated external id' }
      });

      const prismaOrderLineUpdated = await prisma.orderLine.findFirst({ where: { id: orderLineId } });

      expect(prismaOrderLineUpdated.updated_at.getTime()).toBeGreaterThan(
        prismaOrderCreated.order_lines[0].updated_at.getTime()
      );
    });

    it('prisma updateMany should update updated_at timestamp for the related records properly', async () => {
      const order = Order.create(allOrderProps()).getValue();
      await repo.save(order);
      const prismaOrderCreated = await prisma.order.findFirst({
        where: { id: order.id.toValue() },
        include: { order_lines: true }
      });
      expect(prismaOrderCreated.order_lines[0].updated_at).toBeDefined();
      const orderLineId = order.orderLines.getItems()[0].id.toValue();

      await prisma.order.update({
        data: {
          email: 'updated@example.com',
          order_lines: {
            updateMany: {
              data: { external_id: 'updated_id' },
              where: { id: orderLineId }
            }
          }
        },
        where: { id: order.id.toValue() }
      });

      const prismaOrderLineUpdated = await prisma.orderLine.findFirst({ where: { id: orderLineId } });
      expect(prismaOrderLineUpdated.updated_at.getTime()).toBeGreaterThan(
        prismaOrderCreated.order_lines[0].updated_at.getTime()
      );
    });
  });

The first test passed while the second one failed as the updated_at field was not updated.

Expected behavior

The order line record should have an updated value for updated_at field when it was updated in the nested update. But the updated_at field in the order line record does not get updated accordingly even when the other fiedls are updated properly for the order line.

Prisma information

This is the schema,

model Order {
  	id                                   String                  @id
  	external_id                          String? 
	... 
  	order_lines                          OrderLine[]
	created_at                           DateTime                @default(now())
  	updated_at                           DateTime                @default(now()) @updatedAt
}

model OrderLine {
	id 				  String 		    @id
	sku				  String
	external_id	      String
	...
    order_id          String
    created_at        DateTime          @default(now())
    updated_at        DateTime          @default(now()) @updatedAt
    order             Order             @relation(fields: [order_id], references: [id])
}

Environment & setup

  • OS: Mac OS, also on the AWS Lambda runtime
  • Database: Postgres:11 and RDS Aurora 11.9
  • Node.js version: 14.15.4

Prisma Version

prisma               : 2.24.1
@prisma/client       : 2.24.1
Current platform     : darwin
Query Engine         : query-engine 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 18095475d5ee64536e2f93995e48ad800737a9e4 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 18095475d5ee64536e2f93995e48ad800737a9e4
Studio               : 0.397.0
@mick703 mick703 added the kind/bug A reported bug. label Jul 14, 2021
@pantharshit00 pantharshit00 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. labels Jul 26, 2021
@dpetrick dpetrick self-assigned this Jul 28, 2021
@thebiglabasky thebiglabasky added this to the 2.29.0 milestone Jul 28, 2021
@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Aug 1, 2021
@mick703
Copy link
Author

mick703 commented Aug 5, 2021

Thanks guys!

@dpetrick
Copy link
Contributor

dpetrick commented Aug 5, 2021

This will be fixed in 2.29.

@dpetrick dpetrick closed this as completed Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client.
Projects
None yet
Development

No branches or pull requests

4 participants