Issue
I'm using Prisma to generate my database schema, which includes the auto generated default value of ID:
model User {
id Int @id @default(autoincrement())
name String
age Int
}
However, whenever I import the user schema that is generated like :
import { User } from "@prisma/client";
It always has id in the object shape - however, this cannot be used when I am for example, running a create function, as it always complains that id is missing (the create shape should be.
{
"name": "john",
"age": 13
}
Is there anything Prisma generates that doesn't include auto generated values? I could create my own seperate DTO class/interface, however that would mean there isn't a single point of truth for the schema, which i'm not a huge fan of.
Solution
If you run prisma generate
with a schema of User
like the one in the questions, you can run
prisma.user.create({ data: { name: "john", age: 13 } });
without any issues.
You can check UserCreateInput
:
export type UserCreateInput = {
name: string
age: number
}
Just as a side note: You may want to think, if you really want to tie your API definition (DTO, etc.) to the database structure. Although that seems nice on first sight, it introduces strong coupling that might be hindering future enhancements.
Answered By - some-user
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.