Home > Software engineering >  Prisma @updatedAt generates value at creation?
Prisma @updatedAt generates value at creation?

Time:02-11

I tried to follow the prisma getting started here Prisma.

When I first create a user using the prisma client, the updatedAt field already has a value. I assumed it should only generate a value upon update.

Here is the main code

async function main() {
  // ... you will write your Prisma Client queries here

  await prisma.user.create({
    data: {
      email: "[email protected]",
      name: "test",
    },
  });

  const users = await prisma.user.findMany();
  console.log(users);
}

Here is the result when I run the index.ts file

[
  {
    id: 1,
    email: '[email protected]',
    name: 'test',
    createdAt: 2022-02-11T03:49:00.090Z,
    updatedAt: 2022-02-11T03:49:00.090Z
  }
]

CodePudding user response:

Yes, by default Prisma generates @updatedAt value when you create entities.

You can override it on your app side by passing null there (and of course then you need to make field nullable too):

  await prisma.user.create({
    data: {
      email: "[email protected]",
      name: "test",
      // Just pass null here
      updatedAt: null
    },
  });
  • Related