跳到主要內容

自定義模型和欄位名稱

Prisma Client API 是根據您的 Prisma schema 中的模型生成的。模型通常是資料庫表的 1:1 對映。

在某些情況下,特別是使用 內省 時,將資料庫表和列的命名與 Prisma Client API 中使用的名稱解耦可能很有用。這可以透過 Prisma schema 中的 @map@@map 屬性來完成。

您可以使用 @map@@map 分別重新命名 MongoDB 欄位和集合。本頁面使用關係資料庫示例。

示例:關係資料庫

假設您有一個類似於此的 PostgreSQL 關係資料庫 schema

CREATE TABLE users (
user_id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(256),
email VARCHAR(256) UNIQUE NOT NULL
);
CREATE TABLE posts (
post_id SERIAL PRIMARY KEY NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
title VARCHAR(256) NOT NULL,
content TEXT,
author_id INTEGER REFERENCES users(user_id)
);
CREATE TABLE profiles (
profile_id SERIAL PRIMARY KEY NOT NULL,
bio TEXT,
user_id INTEGER NOT NULL UNIQUE REFERENCES users(user_id)
);
CREATE TABLE categories (
category_id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(256)
);
CREATE TABLE post_in_categories (
post_id INTEGER NOT NULL REFERENCES posts(post_id),
category_id INTEGER NOT NULL REFERENCES categories(category_id)
);
CREATE UNIQUE INDEX post_id_category_id_unique ON post_in_categories(post_id int4_ops,category_id int4_ops);

當對具有該 schema 的資料庫進行內省時,您將得到一個類似於此的 Prisma schema

model categories {
category_id Int @id @default(autoincrement())
name String? @db.VarChar(256)
post_in_categories post_in_categories[]
}

model post_in_categories {
post_id Int
category_id Int
categories categories @relation(fields: [category_id], references: [category_id], onDelete: NoAction, onUpdate: NoAction)
posts posts @relation(fields: [post_id], references: [post_id], onDelete: NoAction, onUpdate: NoAction)

@@unique([post_id, category_id], map: "post_id_category_id_unique")
}

model posts {
post_id Int @id @default(autoincrement())
created_at DateTime? @default(now()) @db.Timestamptz(6)
title String @db.VarChar(256)
content String?
author_id Int?
users users? @relation(fields: [author_id], references: [user_id], onDelete: NoAction, onUpdate: NoAction)
post_in_categories post_in_categories[]
}

model profiles {
profile_id Int @id @default(autoincrement())
bio String?
user_id Int @unique
users users @relation(fields: [user_id], references: [user_id], onDelete: NoAction, onUpdate: NoAction)
}

model users {
user_id Int @id @default(autoincrement())
name String? @db.VarChar(256)
email String @unique @db.VarChar(256)
posts posts[]
profiles profiles?
}

當生成 Prisma Client API 時,此 Prisma schema 存在一些“問題”

遵循 Prisma ORM 的命名約定

Prisma ORM 有一個 命名約定,即對 Prisma 模型使用駝峰命名法單數形式。如果未遵循這些命名約定,Prisma schema 可能會變得難以解釋,並且生成的 Prisma Client API 會感覺不那麼自然。考慮以下生成的模型

model users {
user_id Int @id @default(autoincrement())
name String? @db.VarChar(256)
email String @unique @db.VarChar(256)
posts posts[]
profiles profiles?
}

儘管 profiles 指的是一對一關係,但其型別目前以複數形式 profiles 命名,這表明此關係中可能存在許多 profiles。根據 Prisma ORM 約定,模型和欄位理想情況下應按如下方式命名

model User {
user_id Int @id @default(autoincrement())
name String? @db.VarChar(256)
email String @unique @db.VarChar(256)
posts Post[]
profile Profile?
}

由於這些欄位是“Prisma ORM 級別”的 關係欄位,它們不會在底層資料庫中顯現,因此您可以在 Prisma schema 中手動重新命名它們。

帶註解的關係欄位的命名

外部索引鍵在 Prisma schema 中表示為 帶註解的關係欄位 及其對應的關係標量欄位的組合。以下是 SQL schema 中所有關係當前的表示方式

model categories {
category_id Int @id @default(autoincrement())
name String? @db.VarChar(256)
post_in_categories post_in_categories[] // virtual relation field
}

model post_in_categories {
post_id Int // relation scalar field
category_id Int // relation scalar field
categories categories @relation(fields: [category_id], references: [category_id], onDelete: NoAction, onUpdate: NoAction) // virtual relation field
posts posts @relation(fields: [post_id], references: [post_id], onDelete: NoAction, onUpdate: NoAction)

@@unique([post_id, category_id], map: "post_id_category_id_unique")
}

model posts {
post_id Int @id @default(autoincrement())
created_at DateTime? @default(now()) @db.Timestamptz(6)
title String @db.VarChar(256)
content String?
author_id Int?
users users? @relation(fields: [author_id], references: [user_id], onDelete: NoAction, onUpdate: NoAction)
post_in_categories post_in_categories[]
}

model profiles {
profile_id Int @id @default(autoincrement())
bio String?
user_id Int @unique
users users @relation(fields: [user_id], references: [user_id], onDelete: NoAction, onUpdate: NoAction)
}

model users {
user_id Int @id @default(autoincrement())
name String? @db.VarChar(256)
email String @unique @db.VarChar(256)
posts posts[]
profiles profiles?
}

使用 @map@@map 重新命名 Prisma Client API 中的欄位和模型

您可以透過使用 @map@@map 屬性將 Prisma Client 中使用的欄位和模型對映到資料庫中的“原始”名稱來“重新命名”它們。對於上述示例,您可以如下注釋您的模型。

您使用 prisma db pull 內省資料庫後,您可以手動調整生成的 Prisma schema 如下

model Category {
id Int @id @default(autoincrement()) @map("category_id")
name String? @db.VarChar(256)
post_in_categories PostInCategories[]

@@map("categories")
}

model PostInCategories {
post_id Int
category_id Int
categories Category @relation(fields: [category_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
posts Post @relation(fields: [post_id], references: [id], onDelete: NoAction, onUpdate: NoAction)

@@unique([post_id, category_id], map: "post_id_category_id_unique")
@@map("post_in_categories")
}

model Post {
id Int @id @default(autoincrement()) @map("post_id")
created_at DateTime? @default(now()) @db.Timestamptz(6)
title String @db.VarChar(256)
content String?
author_id Int?
users User? @relation(fields: [author_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
post_in_categories PostInCategories[]

@@map("posts")
}

model Profile {
id Int @id @default(autoincrement()) @map("profile_id")
bio String?
user_id Int @unique
users User @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction)

@@map("profiles")
}

model User {
id Int @id @default(autoincrement()) @map("user_id")
name String? @db.VarChar(256)
email String @unique @db.VarChar(256)
posts Post[]
profiles Profile?

@@map("users")
}

透過這些更改,您現在遵循了 Prisma ORM 的命名約定,並且生成的 Prisma Client API 感覺更“自然”

// Nested writes
const profile = await prisma.profile.create({
data: {
bio: 'Hello World',
users: {
create: {
name: 'Alice',
email: 'alice@prisma.io',
},
},
},
})

// Fluent API
const userByProfile = await prisma.profile
.findUnique({
where: { id: 1 },
})
.users()
資訊

prisma db pull 在重新內省資料庫時會保留您在 Prisma schema 中透過 @map@@map 定義的自定義名稱。

重新命名關係欄位

Prisma ORM 級別的 關係欄位(有時被稱為“虛擬關係欄位”)僅存在於 Prisma schema 中,但實際上並未在底層資料庫中顯現。因此,您可以隨意命名這些欄位。

考慮以下 SQL 資料庫中模糊關係的示例

CREATE TABLE "User" (
id SERIAL PRIMARY KEY
);
CREATE TABLE "Post" (
id SERIAL PRIMARY KEY,
"author" integer NOT NULL,
"favoritedBy" INTEGER,
FOREIGN KEY ("author") REFERENCES "User"(id),
FOREIGN KEY ("favoritedBy") REFERENCES "User"(id)
);

Prisma ORM 的內省將輸出以下 Prisma schema

model Post {
id Int @id @default(autoincrement())
author Int
favoritedBy Int?
User_Post_authorToUser User @relation("Post_authorToUser", fields: [author], references: [id], onDelete: NoAction, onUpdate: NoAction)
User_Post_favoritedByToUser User? @relation("Post_favoritedByToUser", fields: [favoritedBy], references: [id], onDelete: NoAction, onUpdate: NoAction)
}

model User {
id Int @id @default(autoincrement())
Post_Post_authorToUser Post[] @relation("Post_authorToUser")
Post_Post_favoritedByToUser Post[] @relation("Post_favoritedByToUser")
}

由於虛擬關係欄位 Post_Post_authorToUserPost_Post_favoritedByToUser 的名稱基於生成的關​​系名稱,它們在 Prisma Client API 中看起來不太友好。在這種情況下,您可以重新命名關係欄位。例如

model Post {
id Int @id @default(autoincrement())
author Int
favoritedBy Int?
User_Post_authorToUser User @relation("Post_authorToUser", fields: [author], references: [id], onDelete: NoAction, onUpdate: NoAction)
User_Post_favoritedByToUser User? @relation("Post_favoritedByToUser", fields: [favoritedBy], references: [id], onDelete: NoAction, onUpdate: NoAction)
}

model User {
id Int @id @default(autoincrement())
writtenPosts Post[] @relation("Post_authorToUser")
favoritedPosts Post[] @relation("Post_favoritedByToUser")
}
資訊

prisma db pull 在重新內省資料庫時會保留在 Prisma schema 中定義的自定義關係欄位。

© . This site is unofficial and not affiliated with Prisma Data, Inc.