From 6fbe505c402cc5b136359eaa77e26e273f77099e Mon Sep 17 00:00:00 2001 From: fly6516 Date: Wed, 14 May 2025 14:02:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor(prisma):=20=E9=87=8D=E6=9E=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=E6=95=B0=E6=8D=AE=E5=8F=8A=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=9A=84=E6=8F=92=E5=85=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 先查找关联的测试用例 - 创建测试用例数据时添加 testcaseId 外键 - 创建测试用例数据配置,关联测试用例数据 ID- 添加了对未找到关联测试用例的错误处理 --- prisma/seed.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/prisma/seed.ts b/prisma/seed.ts index 89f4d51..1a4111e 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1123,21 +1123,42 @@ export async function main() { await prisma.user.create({ data: u }); } - // 插入 TestcaseData 和其配置 for (const item of testcaseDataConfigData) { + // 1. 先找对应的 Testcase + const associatedTestcase = await prisma.testcase.findFirst({ + where: { + data: { + some: { label: item.label }, + }, + }, + }); + if (!associatedTestcase) { + throw new Error(`No associated testcase found for label: ${item.label}`); + } + + // 2. 创建 TestcaseData,带上 testcaseId 外键 const testcaseData = await prisma.testcaseData.create({ data: { label: item.label, value: item.value, index: item.index, + // 直接写外键 + testcaseId: associatedTestcase.id, + // 或者: + // testcase: { connect: { id: associatedTestcase.id } }, }, }); + // 3. 创建 TestcaseDataConfig,此模型只需要 testcaseDataId await prisma.testcaseDataConfig.create({ data: { testcaseDataId: testcaseData.id, type: item.config.type, pattern: item.config.pattern, + // 如果你的 item.config 里还有 min/max/length,也一并写进来: + // min: item.config.min, + // max: item.config.max, + // length: item.config.length, }, }); }