Skip to content

Course 削除時の Cascade を設定 #685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- DropForeignKey
ALTER TABLE "Enrollment" DROP CONSTRAINT "Enrollment_courseId_fkey";

-- DropForeignKey
ALTER TABLE "Slot" DROP CONSTRAINT "Slot_courseId_fkey";

-- AlterTable
ALTER TABLE "Message" ALTER COLUMN "isPicture" DROP DEFAULT;

-- AddForeignKey
ALTER TABLE "Slot" ADD CONSTRAINT "Slot_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Enrollment" ADD CONSTRAINT "Enrollment_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE CASCADE ON UPDATE CASCADE;
4 changes: 2 additions & 2 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ model Course {
// コマ。1つの講義に対して複数存在しうる。
model Slot {
id Int @id @default(autoincrement())
course Course @relation(fields: [courseId], references: [id])
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
courseId String
period Int // 1-6. 0 の場合はなし (集中など)
day Day // 曜日。other の場合は集中など
Expand All @@ -104,7 +104,7 @@ model Enrollment {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
course Course @relation(fields: [courseId], references: [id])
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
courseId String

@@unique([userId, courseId])
Expand Down
83 changes: 83 additions & 0 deletions server/src/seeds/insertCoursesFromJSONByCreateMany.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import * as fs from "node:fs";
import * as path from "node:path";

import { prisma } from "../database/client";

// シ楽バス形式のデータを読み込む。
const FILE_PATH = path.join(__dirname, "data.json");

async function main() {
const jsonData = JSON.parse(fs.readFileSync(FILE_PATH, "utf-8"));

const coursesData = jsonData.map(
(course: {
code: string;
titleJp: string;
lecturerJp: string;
}) => {
const { code, titleJp, lecturerJp } = course;
return {
id: code,
name: titleJp,
teacher: lecturerJp,
};
},
);

await prisma.course.createMany({
data: coursesData,
});

const slotsData: {
day: "mon" | "tue" | "wed" | "thu" | "fri" | "sat" | "sun" | "other";
period: number;
courseId: string;
}[] = [];

for (const courseData of jsonData) {
const { code, periods } = courseData;

for (const period of periods) {
const [dayJp, periodStr] = period.split("");
const day =
dayJp === "月"
? "mon"
: dayJp === "火"
? "tue"
: dayJp === "水"
? "wed"
: dayJp === "木"
? "thu"
: dayJp === "金"
? "fri"
: dayJp === "土"
? "sat"
: dayJp === "日"
? "sun"
: "other";

slotsData.push({
day,
period: Number.parseInt(periodStr) || 0,
courseId: code,
});
}
}

await prisma.slot.createMany({
data: slotsData,
skipDuplicates: true,
});

console.log("Data inserted successfully!");
}

main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});