如何在隐式多对多关系中创建并同时连接到另一个模型?

How to create and at the same time connect to a other model in a Implicit many-to-many relations?

我是 Prisma 的新手。我坚持使用 Prisma.brand.create 方法。这是模型:

model Category {
  id String @id @default(cuid())
  slug String @unique
  name String @unique
  image String?
  createdAt DateTime @default(now())
  user   CMS_users @relation(fields: [userId], references: [id])
  userId String
  brands Brand[]
}

model Brand {
    id String @id @default(cuid())
    slug String @unique
    name String @unique
    image String?
    createdAt DateTime @default(now())
    user   CMS_users @relation(fields: [userId], references: [id])
    userId String
    categories Category[] 
}

这是在 MySQL 中生成的架构:

CREATE TABLE `Brand` (
  `id` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `slug` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `image` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `userId` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Brand_slug_key` (`slug`),
  UNIQUE KEY `Brand_name_key` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `Category` (
  `id` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `slug` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `image` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `userId` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Category_slug_key` (`slug`),
  UNIQUE KEY `Category_name_key` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `_BrandToCategory` (
  `A` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `B` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  UNIQUE KEY `_BrandToCategory_AB_unique` (`A`,`B`),
  KEY `_BrandToCategory_B_index` (`B`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

这里的目标是创建一个品牌,同时将一个或多个类别与该品牌联系起来。

如何在 Prisma 客户端上创建它?现在是这样的:

 prisma.Brand.create({
      data: {
        name: name,
        slug: slug,
        userId: user,
        image: uploadedImage.secure_url,
        categories: {
          create: [
            { category: { connect: { id: "ckzpl0del0014vcv2ctift2f1" } } }
          ]
        }
      },
      include: {
        categories: true
      }
    })

错误显示:

PrismaClientValidationError: 无效 prisma.brand.create() 调用:

{
  data: {
    name: 'wassss',
    slug: 'wassss',
    userId: 'd5eea2a7-78d0-41d9-b090-171c9d7100cc',
    
    categories: {
    ~~~~~~~~~~
      create: [
        {
          category: {
            connect: {
              id: 'ckzpl0del0014vcv2ctift2f1'
            }
          }
        }
      ]
    }
  },
  include: {
    categories: true
  }
}

data.categories 中的 BrandUncheckedCreateInput 类型未知参数 categories。可用参数:

输入 BrandUncheckedCreateInput { id?: 字符串 弹头:字符串 名称:字符串 图像?:字符串|无效的 创建时间?:日期时间 用户ID:字符串 }

你的情况 connectOrCreate API 应该可以解决你的问题。

const result = await prisma.brand.create({
    data: {
      name: "samsung",
      slug: "samsung",
      userId: "xxxx-xxxx-xxxx",
      categories: {
        connectOrCreate: {
          create: {
            name: 'phone',
          },
          where: {
            name: 'phone',
          }
        },
      },
    },
  });

在这种情况下,connectOrCreate 尝试查找名称为 'phone' 的类别,如果找到,则连接到该类别。如果找不到类别,则会创建一个名为 'phone' 的新类别。