我在我的 node.js 專案中使用 prisma ORM。我用 TypeScript 編碼。我已將我的 postgresql 資料庫表反向工程為 prisma 模式。然后我用npx prisma generate. 它創建了我需要使用資料庫的所有型別,但它們不正確。每個表都有一對型別:[tableName]CreateInput和[tableName]uncheckedCreateInput(我不知道這是什么)。
例如我的customers表有customersCreateInput和customersUncheckedCreateInput型別。customersCreateInput缺少id列(此列是最重要的列)。具有包括 在內的customersUncheckedCreateInput所有列id,但它在那里是可選的...為什么 prisma 會這樣做,我該如何解決?我想讓它每次運行時都能正常作業npx prisma generate。請幫幫我,我已經在谷歌搜索了幾天,我找到了一些相應的主題(他們討論了不同的問題,但它是關于 prisma 中未經檢查的型別),但不明白他們在說什么。
export type customersCreateInput = {
customer_email: string
phone: string
first_name: string
middle_name?: string | null
last_name?: string | null
password_hash: string
registered_at: Date | string
last_login?: Date | string | null
comments?: string | null
addresses?: addressesCreateNestedManyWithoutCustomersInput
carts?: cartsCreateNestedManyWithoutCustomersInput
order_items?: order_itemsCreateNestedManyWithoutCustomersInput
orders?: ordersCreateNestedManyWithoutCustomersInput
}
export type customersUncheckedCreateInput = {
id?: number
customer_email: string
phone: string
first_name: string
middle_name?: string | null
last_name?: string | null
password_hash: string
registered_at: Date | string
last_login?: Date | string | null
comments?: string | null
addresses?: addressesUncheckedCreateNestedManyWithoutCustomersInput
carts?: cartsUncheckedCreateNestedManyWithoutCustomersInput
order_items?: order_itemsUncheckedCreateNestedManyWithoutCustomersInput
orders?: ordersUncheckedCreateNestedManyWithoutCustomersInput
}
我用這個 SQL qyery 創建了那個表:
CREATE TABLE Customers (
ID SERIAL PRIMARY KEY NOT NULL,
Customer_Email varchar(50) UNIQUE DEFAULT null,
Phone varchar(25) NOT NULL,
First_Name varchar(40) NOT NULL,
Middle_Name varchar(40) DEFAULT null,
Last_Name varchar(40) DEFAULT null,
Password_Hash varchar(100) NOT NULL,
Registered_At timestamp NOT NULL,
Last_Login timestamp DEFAULT null,
Comments varchar(250) DEFAULT null
);
我生成的棱鏡架構是以下之一:
model customers {
id Int @id @default(autoincrement())
customer_email String @unique @db.VarChar(50)
phone String @db.VarChar(25)
first_name String @db.VarChar(40)
middle_name String? @db.VarChar(40)
last_name String? @db.VarChar(40)
password_hash String @db.VarChar(100)
registered_at DateTime @db.Timestamp(6)
last_login DateTime? @db.Timestamp(6)
comments String? @db.VarChar(250)
addresses addresses[]
carts carts[]
order_items order_items[]
orders orders[]
}
uj5u.com熱心網友回復:
您有@default(autoincrement())for id 因此您可以讓 DB 完成作業而不是在這里手動傳遞它,它將在 DB 級別自動控制以避免沖突并為您簡化事情。
但是如果你想通過id你仍然可以手動完成,這就是為什么第二種型別存在的原因,似乎。
prisma出于某種原因生成兩種不同的型別,并在它們上使用XORtype,因此您可以使用一種或另一種,但不能組合使用它們,如下所示:
export type ExampleCreateArgs = {
/**
* Select specific fields to fetch from the Example
*
**/
select?: ExampleSelect | null
/**
* The data needed to create a Example.
*
**/
data: XOR<ExampleCreateInput, ExampleUncheckedCreateInput>
}
例如,如果您想data在代碼中重用該型別,您可以執行以下操作:
// substitute ExampleCreateArgs to correct one from your code
type Data = ExampleCreateArgs["data"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/411521.html
標籤:
