From 49d96b77e83631ebdcbf860d9d7b5171073aa30c Mon Sep 17 00:00:00 2001 From: Toan Hoang Minh Date: Fri, 31 Jul 2026 14:58:47 +0700 Subject: [PATCH] feat: relate Request.employee_id to Employee via FK Retrofits Request.employeeId from a free-text String with no referential integrity to a real relation: employeeId String @db.Uuid + employee Employee @relation(...), plus the inverse Employee.requests back-relation field. Migration alters requests.employee_id from TEXT to UUID with an FK constraint (drop+recreate, confirmed empty table - no production/dev data existed to migrate). --- .../migration.sql | 12 ++++++++++++ prisma/schema.prisma | 9 ++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20260731075828_relate_request_to_employee/migration.sql diff --git a/prisma/migrations/20260731075828_relate_request_to_employee/migration.sql b/prisma/migrations/20260731075828_relate_request_to_employee/migration.sql new file mode 100644 index 0000000..2422678 --- /dev/null +++ b/prisma/migrations/20260731075828_relate_request_to_employee/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - Changed the type of `employee_id` on the `requests` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + +*/ +-- AlterTable +ALTER TABLE "requests" DROP COLUMN "employee_id", +ADD COLUMN "employee_id" UUID NOT NULL; + +-- AddForeignKey +ALTER TABLE "requests" ADD CONSTRAINT "requests_employee_id_fkey" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 18c65c7..eaed748 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -27,7 +27,13 @@ enum RequestType { model Request { id String @id @default(uuid()) @db.Uuid - employeeId String @map("employee_id") + // Was a free-text String with no referential integrity — retrofitted to a + // real FK once Employee records existed to point to (see Employee model + // above). No production data existed at the time of this change (`requests` + // table was empty), so this was a plain column-type migration, not a + // backfill. + employeeId String @map("employee_id") @db.Uuid + employee Employee @relation(fields: [employeeId], references: [id]) type RequestType reason String // Only the 'Draft' state is implemented so far. Deliberately kept as a @@ -51,6 +57,7 @@ model Employee { managerId String? @map("manager_id") @db.Uuid manager Employee? @relation("EmployeeManager", fields: [managerId], references: [id]) reports Employee[] @relation("EmployeeManager") + requests Request[] createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6) @@map("employees")