Ensuring data type correctness when creating a Prisma model named 'bid' is crucial. With auto-generated prisma types available, understanding the naming convention and selecting the appropriate type can be confusing.
The bid schema looks like this:
model Bid {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
user User @relation(fields: [userId], references: [id])
userId String
auction Auction @relation(fields: [auctionId], references: [id])
auctionId Int
amount Int @default(0)
@@index([userId])
@@index([auctionId])
}
Although the code functions as intended, adding type safety to the data
object is desired.
const data = {
userId,
auctionId,
amount,
};
await prisma.bid.create({
data,
});
Attempting to use intellisense on bid.create
produces unclear type information:
(method) Prisma.BidDelegate<false, DefaultArgs>.create<{
data: {
userId: any;
auctionId: any;
amount: any;
};
}>(args: {
data: {
userId: any;
auctionId: any;
amount: any;
};
// blah blah
No specific types are provided.
Trying to extract BidCreateArgs
from the Prisma object results in an error:
import { Prisma } from "@prisma/client";
...
const data: Prisma.BidCreateArgs = {
userId,
auctionId,
amount,
};
The error message reads:
Type 'BidCreateArgs<DefaultArgs>' is not assignable to type '(Without<BidCreateInput, BidUncheckedCreateInput> & BidUncheckedCreateInput) | (Without<BidUncheckedCreateInput, BidCreateInput> & BidCreateInput)'.
Similarly using BidCreateInput
yields more detailed errors.
Given the model name, what type should be used for creation?
It's worth noting that there are different parent imports available, perhaps the incorrect one was chosen?
import { Prisma } from "@prisma/client";
import { PrismaClient } from "@prisma/client";