Prisma2 prisma introspect 为外键返回奇怪的值
Prisma2 prisma introspect returning weird values for foreign keys
我有两个表,User
和 Relationship
。这些表用于存储父子关系。我正在使用 Postgres
// schema.sql
CREATE TABLE "public"."Relationships" (
id SERIAL PRIMARY KEY NOT NULL,
parent_id INT NOT NULL,
FOREIGN KEY (parent_id) REFERENCES "User" (id),
child_id INT NOT NULL,
FOREIGN KEY (child_id) REFERENCES "User" (id)
)
CREATE TABLE "public"."User" (
id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(128) NOT NULL,
email VARCHAR(128) UNIQUE,
password VARCHAR(128) NOT NULL,
isChild BOOLEAN NOT NULL DEFAULT false
created_at TIMESTAMP NOT NULL DEFAULT NOW();
);
当我运行 npx prisma introspect
schema.prisma
文件中返回以下内容。
// schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Relationships {
child_id Int
id Int @default(autoincrement()) @id
parent_id Int
User_Relationships_child_idToUser User @relation("Relationships_child_idToUser", fields: [child_id], references: [id])
User_Relationships_parent_idToUser User @relation("Relationships_parent_idToUser", fields: [parent_id], references: [id])
}
model User {
created_at DateTime @default(now())
email String? @unique
id Int @default(autoincrement()) @id
ischild Boolean @default(false)
name String?
password String
Relationships_Relationships_child_idToUser Relationships[] @relation("Relationships_child_idToUser")
Relationships_Relationships_parent_idToUser Relationships[] @relation("Relationships_parent_idToUser")
}
我不明白 User_Relationships_child_idToUser
和 User_Relationships_parent_idToUser
是什么,为什么它们不只是 Prisma 文档教程中针对外键出现的简单语法。
Prisma 的内省为每个外键生成两个字段:
- 关系标量字段(基本上是外键的直接表示)
- 一个关系字段(用
@relation
属性注释)——这些字段似乎让您感到困惑。他们提供的原因是您可以在 Prisma Client API. 中轻松处理这些关系
您可以在文档中找到更多相关信息 here。
当然User_Relationships_parent_idToUser
和User_Relationships_child_idToUser
不是很好听的名字。您可以在内省后手动调整架构,rename the relation fields 以获得更友好的名称,例如:
model Relationships {
child_id Int
id Int @default(autoincrement()) @id
parent_id Int
child User @relation("Relationships_child_idToUser", fields: [child_id], references: [id])
parent User @relation("Relationships_parent_idToUser", fields: [parent_id], references: [id])
}
我有两个表,User
和 Relationship
。这些表用于存储父子关系。我正在使用 Postgres
// schema.sql
CREATE TABLE "public"."Relationships" (
id SERIAL PRIMARY KEY NOT NULL,
parent_id INT NOT NULL,
FOREIGN KEY (parent_id) REFERENCES "User" (id),
child_id INT NOT NULL,
FOREIGN KEY (child_id) REFERENCES "User" (id)
)
CREATE TABLE "public"."User" (
id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(128) NOT NULL,
email VARCHAR(128) UNIQUE,
password VARCHAR(128) NOT NULL,
isChild BOOLEAN NOT NULL DEFAULT false
created_at TIMESTAMP NOT NULL DEFAULT NOW();
);
当我运行 npx prisma introspect
schema.prisma
文件中返回以下内容。
// schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Relationships {
child_id Int
id Int @default(autoincrement()) @id
parent_id Int
User_Relationships_child_idToUser User @relation("Relationships_child_idToUser", fields: [child_id], references: [id])
User_Relationships_parent_idToUser User @relation("Relationships_parent_idToUser", fields: [parent_id], references: [id])
}
model User {
created_at DateTime @default(now())
email String? @unique
id Int @default(autoincrement()) @id
ischild Boolean @default(false)
name String?
password String
Relationships_Relationships_child_idToUser Relationships[] @relation("Relationships_child_idToUser")
Relationships_Relationships_parent_idToUser Relationships[] @relation("Relationships_parent_idToUser")
}
我不明白 User_Relationships_child_idToUser
和 User_Relationships_parent_idToUser
是什么,为什么它们不只是 Prisma 文档教程中针对外键出现的简单语法。
Prisma 的内省为每个外键生成两个字段:
- 关系标量字段(基本上是外键的直接表示)
- 一个关系字段(用
@relation
属性注释)——这些字段似乎让您感到困惑。他们提供的原因是您可以在 Prisma Client API. 中轻松处理这些关系
您可以在文档中找到更多相关信息 here。
当然User_Relationships_parent_idToUser
和User_Relationships_child_idToUser
不是很好听的名字。您可以在内省后手动调整架构,rename the relation fields 以获得更友好的名称,例如:
model Relationships {
child_id Int
id Int @default(autoincrement()) @id
parent_id Int
child User @relation("Relationships_child_idToUser", fields: [child_id], references: [id])
parent User @relation("Relationships_parent_idToUser", fields: [parent_id], references: [id])
}