Is Prisma's @updatedAt supported?
Closed this issue · 2 comments
Thank you for all your hard work on this library @valtyr!
I have a Prisma schema that uses the @updatedAt
function for an updatedAt
column, and on the application side if I'm inserting a record into the table and don't specify a value for this column, TypeScript will complain saying this field is required.
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
generator kysely {
provider = "prisma-kysely"
output = "./kysely"
fileName = "types.ts"
previewFeatures = ["jsonProtocol"]
}
model User {
id String @id @default(dbgenerated("gen_random_uuid()"))
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
posts Post[]
@@map("users")
}
I'm using MySQL through PlanetScale with kysely-planetscale
. The kysely generated type looks as follows:
export type User = {
id: string
email: string
createdAt: Generated<Timestamp>
updatedAt: Timestamp
}
At the app level, here's where the error shows up:
await ctx.db
.insertInto('users')
.values({
id: generateNanoId(),
email: input.email,
// updatedAt: new Date(),
})
.execute()
Without manually writing the updatedAt: new Date()
field, I get this error:
Property 'updatedAt' is missing in type '{ id: string; email: string; }' but required in type '{ email: ValueExpression<DB, "users", string>; id: ValueExpression<DB, "users", string>; updatedAt: ValueExpression<DB, "users", string | Date>; }'.
Is this expected behavior? Or am I doing something wrong here / is there a way around this to avoid writing the updatedAt
field manually every time?
This is expected behavior since Prisma implements @updatedAt
typescript-side, not database side. This is similar to the limitation with Prisma's ID types. If you were to write a manual insert against the database it would require you to supply the updatedAt
field. You could possibly define a Postgres trigger that updates this field on updates, and add a default value to it, but I'm not sure Prisma supports this in their schema language.
@uicrafts I fork and add updatedAt feature in my package.
https://www.npmjs.com/package/@if1live/prisma-kysely
.... after I use my forked package. I understand valtyr's choice.
my schema.prisma
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
... and insane? solution (for mysql only)
prisma
created_at DateTime @default(now())
updated_at DateTime @default(dbgenerated("CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)")) @updatedAt
sql
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),