【问题标题】:prisma introspect generate invalid modelprisma introspect 生成无效模型
【发布时间】:2021-09-20 13:05:19
【问题描述】:

棱镜版本:^2.26.0

Prisma/客户端:^2.26.0

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


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

由于我的代码中有这些错误,导致无法进入 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
}

【问题讨论】:

  • 能否请您提供更多有关该问题的背景信息。这里有几件事会有所帮助: 1. 您使用的是哪个版本的 prisma 和 prisma studio? 2. prisma studio 显示的具体错误是什么? 3. 是工作室的问题还是客户端的问题? 4. 可以使用 prisma 客户端对自省模型进行 CRUD 操作吗?
  • posts.userId 和 users.id 必须都是 Bigint ?

标签: sql postgresql prisma


【解决方案1】:

只需在 Post Model 中将 Int 更改为 BigInt userId 即可

【讨论】:

  • 谢谢!我稍后再试!
  • 是的,它有效!我能知道 Int 不可接受的原因是什么吗?
  • 类型问题,在模型用户中您提到 id 作为 BidInt 但在 Post 模型中您提到 userId Int,这就是原因,如果我的答案有效,请将我的答案标记为已解决,以便其他人让它有效.
猜你喜欢
  • 2020-08-08
  • 2022-08-22
  • 1970-01-01
  • 2021-12-29
  • 2021-02-09
  • 2022-10-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
相关资源
最近更新 更多