const { PrismaClient } = require('@prisma/client'); const prisma = new PrismaClient(); async function checkParentId() { const bookId = 2; // 查询前3个章 const chapters = await prisma.bookChapter.findMany({ where: { bookId, level: 1 }, orderBy: { number: 'asc' }, take: 2 }); console.log('=== Level 1 (章) ==='); chapters.forEach(c => { console.log(`id=${c.id}, parentId=${c.parentId}, number=${c.number}, title=${c.title.substring(0, 20)}`); }); if (chapters.length > 0) { const firstChapterId = chapters[0].id; // 查询这个章的节 const sections = await prisma.bookChapter.findMany({ where: { bookId, level: 2, parentId: firstChapterId }, orderBy: { number: 'asc' }, take: 3 }); console.log(`\n=== Level 2 (节, parentId=${firstChapterId}) ===`); sections.forEach(s => { console.log(`id=${s.id}, parentId=${s.parentId}, number=${s.number}, title=${s.title.substring(0, 20)}`); }); if (sections.length > 0) { const firstSectionId = sections[0].id; // 查询这个节的小节 const subsections = await prisma.bookChapter.findMany({ where: { bookId, level: 3, parentId: firstSectionId }, orderBy: { number: 'asc' }, take: 3 }); console.log(`\n=== Level 3 (小节, parentId=${firstSectionId}) ===`); subsections.forEach(sub => { console.log(`id=${sub.id}, parentId=${sub.parentId}, number=${sub.number}, title=${sub.title.substring(0, 20)}`); }); } } await prisma.$disconnect(); } checkParentId().catch(console.error);