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

Ignore certain fields for @updatedAt #24080

Open
SuperManito opened this issue May 5, 2024 · 5 comments
Open

Ignore certain fields for @updatedAt #24080

SuperManito opened this issue May 5, 2024 · 5 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/schema Issue for team Schema. topic: update() topic: updatedAt

Comments

@SuperManito
Copy link

Problem

For example, I have a status field and an update_time field, and when I change the status value, I expect that the value of the update_time field will not be automatically updated.

model envs {
  id          Int         @id @default(autoincrement())
  update_time DateTime    @default(now()) @updatedAt
  value       String      @default("")
  status      Int      @default(1)
}

Suggested solution

@updatedAtIgnoreFields(['status'])

@marcus13371337
Copy link

marcus13371337 commented May 8, 2024

Pro-tip: you can manually opt-out from updatedAt is updating by providing prisma with the previous value.

I.e:

const env = await prisma.envs.findFirst({})

const envUpdated = await prisma.envs.update({
    where: {
       id: env.id
    },
    data: {
        status: "new status",
        update_time: env.update_time
    }
})

// envUpdated.update_time should be the same as env.update_time

@Druue
Copy link
Contributor

Druue commented May 8, 2024

Hey @SuperManito, can you clarify please:

@updatedAt triggers whenever the model is updated at all. Are you asking for a way to have the @updatedAt attribute to only be used when certain values are affected in .update queries? (i.e. a feature request? the way this is working in your issue is currently intended)

@Druue Druue added team/schema Issue for team Schema. team/client Issue for team Client. topic: updatedAt topic: update() kind/feature A request for a new feature. labels May 8, 2024
@SuperManito
Copy link
Author

My project uses a unified prisma encapsulation operation method and has multiple data models. It is difficult for me to find another way to solve this requirement.

@SuperManito
Copy link
Author

It is difficult to solve this problem using a where statement because it is too cumbersome to customize.

@janpio janpio changed the title Need ignoring fields of @updatedAt schema Ignore certain fields for @updatedAt May 8, 2024
@erikmusd
Copy link

erikmusd commented May 16, 2024

Pro-tip: you can manually opt-out from updatedAt is updating by providing prisma with the previous value.

I.e:

const env = await prisma.envs.findFirst({})

const envUpdated = await prisma.envs.update({
    where: {
       id: env.id
    },
    data: {
        status: "new status",
        update_time: env.update_time
    }
})

// envUpdated.update_time should be the same as env.update_time

Though would this fire off 2 separate queries? Finding then later updating, or is Prisma smart enough to combine into single call? I'm not familiar with Prisma internals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/schema Issue for team Schema. topic: update() topic: updatedAt
Projects
None yet
Development

No branches or pull requests

4 participants