Prisma introspect 生成无效模型

prisma introspect generate invalid model

Prisma 版本:^2.26.0

Prisma/client : ^2.26.0

@relation 错误消息:解析属性“@relation”时出错:模型 posts 中的字段类型 userId 与引用字段的类型不匹配 id 在模型中 users.


为什么 prisma 在我 运行 npx prisma introspect?[=16 之后创建了无效模型或我的代码有问题=]

由于我的代码中出现这些错误,导致无法进入 prisma studio

任何解决方案或想法可以分享让我更好地了解这个?

下面是我的sql:

-- Create a custom type
CREATE TYPE "user_role_enum" AS ENUM ('user', 'admin', 'superadmin');

-- Create a table
CREATE TABLE "users"(
    "id" BIGSERIAL PRIMARY KEY NOT NULL,
    "name" VARCHAR(255) NOT NULL,
    "email" VARCHAR(255) UNIQUE NOT NULL,
    "role" user_role_enum NOT NULL DEFAULT('user')
);

-- Create a table
CREATE TABLE "posts"(
    "id" BIGSERIAL PRIMARY KEY NOT NULL,
    "title" VARCHAR(255) NOT NULL,
    "body" TEXT,
    "userId" INTEGER NOT NULL,
    FOREIGN KEY ("userId") REFERENCES "users"("id")
);

-- Create data
INSERT INTO "users" ("name", "email", "role")
VALUES('John Doe', 'john@email.com', 'admin'),
('jane Doe', 'jane@email.com', 'admin'),
('Ahmed Hadjou', 'ahmed@hadjou.com', 'user');

INSERT INTO "posts" ("title", "body", "userId")
VALUES('Hello World!!', 'Hey guys, I see a rainbow through this prisma :D', 1),
('So do I', 'It looks cooool!!!', 2),
('It does', 'Yeahhh', 1);

从 prisma introspect 生成模型


model posts {
  id     BigInt  @id @default(autoincrement())
  title  String  @db.VarChar(255)
  body   String?
  userId Int
  users  users   @relation(fields: [userId], references: [id]) // Error parsing attribute "@relation": The type of the field `userId` in the model `posts` is not matching the type of the referenced field `id` in model `users`.
}

model users {
  id    BigInt         @id @default(autoincrement())
  name  String         @db.VarChar(255)
  email String         @unique @db.VarChar(255)
  role  user_role_enum @default(user)
  posts posts[] // error in here
}

enum user_role_enum {
  user
  admin
  superadmin
}

只需将 Post 模型中的 Int 更改为 BigInt userId,它就可以工作