From d6ef23d01b07f31b2aba4d6246be4cac218a212a Mon Sep 17 00:00:00 2001 From: fuomag9 <1580624+fuomag9@users.noreply.github.com> Date: Sun, 2 Nov 2025 22:51:27 +0100 Subject: [PATCH] Fix foreign key constraint error when creating proxy hosts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The issue occurred because the auth system uses a hardcoded JWT user ID (1) that didn't exist in the database, causing foreign key constraint violations when creating proxy hosts. Changes: - Added init-db.ts to ensure admin user exists in database - Added instrumentation.ts to run DB initialization on server startup - Admin user (from ADMIN_USERNAME env var) is now created with ID 1 - Matches the hardcoded ID in auth.ts for JWT tokens Fixes foreign key constraint error (P2003) when creating proxy hosts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- next-env.d.ts | 2 +- src/instrumentation.ts | 18 ++++++++++++++ src/lib/init-db.ts | 55 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/instrumentation.ts create mode 100644 src/lib/init-db.ts diff --git a/next-env.d.ts b/next-env.d.ts index c4b7818f..9edff1c7 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -1,6 +1,6 @@ /// /// -import "./.next/dev/types/routes.d.ts"; +import "./.next/types/routes.d.ts"; // NOTE: This file should not be edited // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/src/instrumentation.ts b/src/instrumentation.ts new file mode 100644 index 00000000..41d061d7 --- /dev/null +++ b/src/instrumentation.ts @@ -0,0 +1,18 @@ +/** + * Next.js instrumentation hook - runs once when the server starts + * https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation + */ +export async function register() { + // Only run on the server side + if (process.env.NEXT_RUNTIME === "nodejs") { + const { ensureAdminUser } = await import("./lib/init-db"); + + try { + await ensureAdminUser(); + console.log("Database initialization complete"); + } catch (error) { + console.error("Failed to initialize database:", error); + // Don't throw - let the app start anyway, errors will surface when users try to use features + } + } +} diff --git a/src/lib/init-db.ts b/src/lib/init-db.ts new file mode 100644 index 00000000..c48a47ba --- /dev/null +++ b/src/lib/init-db.ts @@ -0,0 +1,55 @@ +import prisma, { nowIso } from "./db"; +import { config } from "./config"; + +/** + * Ensures the admin user from environment variables exists in the database. + * This is called during application startup. + */ +export async function ensureAdminUser(): Promise { + const adminId = 1; // Must match the hardcoded ID in auth.ts + const adminEmail = `${config.adminUsername}@localhost`; + const provider = "credentials"; + const subject = config.adminUsername; + + // Check if admin user already exists + const existingUser = await prisma.user.findUnique({ + where: { id: adminId } + }); + + if (existingUser) { + // Admin user exists, update if needed + if (existingUser.email !== adminEmail || existingUser.subject !== subject) { + const now = new Date(nowIso()); + await prisma.user.update({ + where: { id: adminId }, + data: { + email: adminEmail, + subject, + updatedAt: now + } + }); + console.log(`Updated admin user: ${config.adminUsername}`); + } + return; + } + + // Create admin user + const now = new Date(nowIso()); + await prisma.user.create({ + data: { + id: adminId, + email: adminEmail, + name: config.adminUsername, + passwordHash: null, // Using environment variable auth, not password hash + role: "admin", + provider, + subject, + avatarUrl: null, + status: "active", + createdAt: now, + updatedAt: now + } + }); + + console.log(`Created admin user: ${config.adminUsername}`); +}